Shell scripting short tip: Timeout!

There’s your cron job. It’s been running for ages. You want it to stop. Better still, you want it to timeout after a certain amount of time.

You’ll be pleased to know there’s a timeout command available on coreutils (for Linux distros).

Now your cron can be something like

*/5 * * * * timeout –kill-after=290s 4m my-script.sh

This runs every 5 minutes. After 4 minutes the timeout utility sends a TERM signal to my-script.sh. Then at the 290 seconds mark, it sends a KILL in case TERM didn’t do its job; this might be the case if your script / program either blocks or handles TERM signal.

For more information, please head to its man page at http://man7.org/linux/man-pages/man1/timeout.1.html

Have a good one!

Leave a comment