在 Unix 中安排作业

Schedule a job in Unix

我对 Unix 环境还很陌生。

我正在尝试在 Unix 服务器中安排两个任务。第二个任务取决于第一个任务的结果。所以,我要运行第一个任务。如果没有错误,那么我希望第二个任务自动 运行 。但是如果第一个任务失败了,我想在30分钟后重新安排第一个任务。

我不知道从哪里开始。

您不需要 cron。您只需要一个简单的 shell 脚本:

#!/bin/sh

while :; do             # Loop until the break statement is hit
   if task1; then       # If task1 is successful
      task2             # then run task2
      break             # and we're done.
   else                 # otherwise task1 failed
      sleep 1800        # and we wait 30min
   fi                   # repeat
done

请注意,task1 必须用退出状态 0 表示 成功 ,用非零表示失败。

正如 Wumpus 敏锐地观察到的,这可以简化为

#!/bin/sh
until task1; do
   sleep 1800
done
task2