
command/script
當主要任務command/script
(任務 A)超出定義的時間視窗/週期時,如何以平行模式執行不同的任務(任務 B);中提到了哪一個crontab
?
@生產環境,沒有gnome-terminal
.
答案1
預設情況下會發生這種情況。計劃運行全部大約在同一時間安排在給定分鐘內進行的作業。沒有隊列,也絕對沒有時間窗口/時段。只有一組開始時間。
答案2
正如 l0b0 所提到的在他的回答中,crontab檔案只指定作業的開始時間。它並不關心作業是否需要花費幾個小時來運行,並且會在下一個開始時間到達時再次啟動它,即使作業的前一個版本仍在運行。
根據您的描述,如果任務 A 運行時間太長,您似乎希望啟動任務 B。
您可以透過將兩個任務組合在一個同一個腳本中來實現這一目標:
#!/bin/sh
timeout=600 # time before task B is started
lockfile=$(mktemp)
trap 'rm -f "$lockfile"' EXIT INT TERM QUIT
# Start task A
# A "lock file" is created to signal that the task is still running.
# It is deleted once the task has finished.
( touch "$lockfile" && start_task_A; rm -f "$lockfile" ) &
task_A_pid="$!"
sleep 1 # allow task A to start
# If task A started, sleep and then check whether the "lock file" exists.
if [ -f "$lockfile" ]; then
sleep "$timeout"
if [ -f "$lockfile" ]; then
# This is task B.
# In this case, task B's task is to kill task A (because it's
# been running for too long).
kill "$task_A_pid"
fi
fi