在执行 crontab 内的指令时, 如果想要限制执行时间, 可以使用 timeout 指令完成, 而这个方法不仅适用于 crontab, 即是一般指令也适用。
在 man 查看 timeout 指令的用法:
timeout – run a command with a time limit
再看看 crontab 的部份, 例如原来有一个 Shell Script 在 crontab 内执行:
# crontab -l
*/5 * * * * /path/to/script.sh
*/5 * * * * /path/to/script.sh
以上看到 /path/to/script.sh 会每 5 分钟执行一次, 将以上内容加上 /usr/bin/timeout, 设定为执行限时 60 秒:
To:
*/5 * * * * /usr/bin/timeout 60 /path/to/script.sh
当 /path/to/script.sh 执行超过 60 秒时, timeout 对 script.sh 发出 kill signal.