Using hatools in cron
A typical usage of halockrun is to make sure a program which is triggered by cron is not triggered unless the previous run has finished. Consider the following crontab entry:
* * * * * program args
cron will start your program every minute. If your program needs more then a minute to finish, cron will start a second instance while the first is still running. If you have bad luck, you will have your program running quite a lot of times in parallel. If you just want your program not to start if the previous execution has not yet finished, you can use halockrun like this:
* * * * * halockrun -nc /tmp/lockfile program args
cron will start halockrun every minute, if the specified lockfile is busy right now, halockrun will just terminate, otherwise it will aquire the lock and start your program. The lock is held until the program terminates.
Everytime when you use a lock, you must be concerned about stale locks. halockrun is very immune to stale locks in case your program crashes (e.g. core dump), since the kernel is responsible to relase the lock. Whenever your program ends (no matter in which way) the kernel will release the lock. However, if you program just never ends we have a problem. For that reason there is the companion tool hatimerun. hatimerun can be used to automatically kill a program when it does not finish within a given time. e.g. if you are sure that there is something wrong if your program doesn't finish within 5 minutes, you can use this:
* * * * * hatimerun -t 300 halockrun -nc /tmp/lockfile program args
This will cause hatimerun to wait for 300 seconds (recent hatools version also accept -t 5:0 for 5 minutes) and then send the TERM signal to your program. The halockrun mechanism will make sure the lock is properly released.