単一の Cron ジョブからの並列スクリプトの実行

単一の Cron ジョブからの並列スクリプトの実行

command/scriptプライマリ タスク (タスクcommand/scriptA) が定義済みの時間ウィンドウ/期間を超えている間に、別のタスク (タスク B) を並列モードで実行するにはどうすればよいですcrontabか。

@ 実稼働環境、ありませんgnome-terminal

答え1

これはデフォルトで実行されます。Cronは全てほぼ同時に特定の分にスケジュールされているジョブ。キューはなく、時間枠/期間は絶対にありません。開始時刻のセットのみがあります。

答え2

l0b0が言ったように彼の答えの中でcrontab ファイルはジョブの開始時刻のみを指定します。ジョブの実行に何時間もかかるかどうかは関係なく、前のジョブがまだ実行中であっても、次の開始時刻になると問題なくジョブが再開されます。

あなたの説明からすると、タスク A の実行に時間がかかりすぎる場合にタスク B を開始したいようですね。

これを実現するには、次の 2 つのタスクを 1 つのスクリプトに組み合わせます。

#!/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

関連情報