7

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.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77

2 Answers2

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