Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

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.

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

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.