Limiting command runtime in Linux
It is sometimes useful to limit the running time of a process, either to stop it from using up all resources or to make sure nightly cron jobs don’t continue into working hours.
I needed this for rsync, to let a remote backup server slowly catch up if large amounts of data were added to the live server during the day. A useful post on the rsync mailing list discusses an rsync patch but also the timeout
command.
After installing (the Debian package is simply timeout
) it is as easy as running with the number of seconds to run for:
timeout 21600 rsync -a ...
It is also possible to specify the signal which will be sent to a program, which is useful if you do not want to simply send SIGKILL. I used SIGHUP in the hope that rsync would have a chance to exit gracefully:
timeout -1 21600 rsync -a ...
A full list of signals and their numeric values can be found in man 1 kill
.
A wrapper script is also available from Johannes Buchner.