9

I've got a cronjob which runs hourly that is occasionally taking too long to run, is there any way I can set a maximum runtime and the job gets killed if it exceeds that?

This is on an Ubuntu 10.04 server.

Thanks, jebw

3 Answers 3

11

Try the timeout command. For example:

0 * * * * timeout -2 3540 /path/to/your_command.sh

will send a SIGINT to your command if it hasn't completed in 59 minutes.

6
  • Good info. You missed '-s', but it is "timeout -s 2 3540 <command>". Apr 8, 2011 at 14:26
  • The one installed via apt on ubuntu uses the syntax I mentioned: usage: timeout [-signal] time command...
    – Cakemox
    Apr 8, 2011 at 14:32
  • What does 'dpkg -S bin/timeout' gives? In fact mine is from coreutils and is pure GNUish. Apr 8, 2011 at 14:40
  • timeout: /usr/bin/timeout
    – Cakemox
    Apr 8, 2011 at 15:00
  • 10.04, same as the OP.
    – Cakemox
    Apr 8, 2011 at 16:37
0

Nope. The way we do it is to make a script with a lock file and have the script check for that lock file before start running again, so it can check if it is already running and decide if it runs, if it waits for the other to end or if it kills the other before running.

A lock file is just a empty file or a file with the other script PID saved somewhere like /var/run or /var/lock.

0

cakemox's answer is the best. Otherwise, just put the pid intoa file or use killall on anouther cronjob a minute earlier to kill the process.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .