I have a scheduled cron job (which is actually a shell script). I'd like to limit the script execution time as it can work unacceptably long. For some reason I can not limit the script execution time from inside the script. Actually, I want my system to force the task termination if it runs more than N hours. Please advise.
Asked
Active
Viewed 6,857 times
7
-
Maybe this helps http://stackoverflow.com/questions/2387485/limiting-the-time-a-program-runs-in-linux – dwalter Aug 30 '12 at 11:26
-
2See [Can I limit the runtime of a cronjob](http://serverfault.com/a/257347) over at serverfault.com. – Christian.K Aug 30 '12 at 11:41
2 Answers
21
I'm responding by myself just because none answered.
0 * * * * timeout -s 9 3540 /path/to/your_command.sh
will send a SIGINT to your command if it hasn't completed in 59 minutes.

Maksym Polshcha
- 18,030
- 8
- 52
- 77
-
not works at me `*/2 * * * * /usr/bin/timeout -s 9 60 && cd /usr/etc && python2.6 hello.py 10.10.252.120 437 >> res.txt ` – Yohanim May 14 '18 at 07:14
7
Using an -s 9 is a little extreme as it doesn't let the process do any cleanup. Yes, it will kill the process if it can be killed, but > 90% of the time a -s 1, which requests a graceful hangup, will suffice.

Larry Dillon
- 71
- 1
- 1