Scheduling

如何重新安排 at 作業以提前執行?

  • July 20, 2021

假設我安排了一個at作業在 3 小時後執行:

$ echo command | at now +3 hours
$ atq
9     Mon Dec  5 14:00:00 2016 a nr

但是經過 1 小時後,我改變了主意,然後需要立即#9從隊列中執行該特定作業a,即比計劃執行時間早 2 小時。

我該怎麼做?

我知道我可以將作業命令列印到stdout,將其複制並粘貼到命令行,手動執行,然後刪除作業#9

$ at -c 9
command
$ command
$ atrm 9

但這相當於執行另一個作業,而不是#9從 queue a

有兩種可能:

at -c 9 | at now + 1 hour -- reschedule job 9 from whenever to now + 1 hour
atrm 9                    -- Removed the old job

根據接受的答案,這是一個方便的腳本(我稱之為atmv):

#!/usr/bin/bash

# Idea taken from here:
# https://unix.stackexchange.com/a/331789/68456

set -euo pipefail

atlist() { atq | sed 's/^/  /'; }

if [ $# -lt 2 ]; then
   echo "Syntax: atmv job_num new time arguments"
   exit 1
fi

job_num="$1"
shift

if [ "$(atq | cut -f 1 | grep "$job_num" | wc -l)" != "1" ]; then
   echo "Error: There is no job with number \"$job_num\"."
   echo "Pick one of these:"
   atlist
   exit 2
fi

at -c "$job_num" | at "$@" 2> /dev/null
atrm "$job_num"
echo "Done! This is the new list of at jobs:"
atlist

引用自:https://unix.stackexchange.com/questions/328128