Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm run a script with cron:

*/10 * * * * flock -n /tmp/lock script

I have to make sure that only one instance of the script is running at the same time, and for that I'm using flock. The problem is that sometimes this script starts a daemon, in this case, the daemon blocks the following script executions.

I know a possible solution would be unlock the file at the end of the script, but is it possible do it directly in the cron command?

share|improve this question
    
if the script starts a daemon, does it get the daemon to go into the background and detach itself from it? i.e. does the script itself still complete? –  drone.ah Feb 16 '13 at 11:10
    
the script starts the daemon and exits, and I think detach itself from it. The script is a python script and uses subprocess.call() to start the daemon –  José Luis Feb 16 '13 at 18:06
    
documentation for call says that the process waits for the call to complete. If the daemon does not return immediately, that could be why you have this issue. Can you change the script to fork the daemon instead and see if that does it? –  drone.ah Feb 16 '13 at 19:05

1 Answer 1

It seems you want keep your daemon running by cron. If yes, it isn't good idea. Why this flock sometimes fails? You use /tmp directory. Maybe another process removes old files. When it is done, flock still keeps flocked but deleted files. On another 10min cron round another flock instance doesn't see this file, creates another and run unneeded duplicate. Resolution, move your lock file into /var/run directory or elsewhere. If your daemon often ends working, consider exit codes. Assign some exit code for signal INT15. Run your daemon one from some 'infinite' loop. This loop should detect, your daemon is killed by sig15 or other. when other, do simply another loop. if 15, leave loop and end your daemon live. you can use runsv package, available under debian and redhat. it work completly different to cron. it run your script/daemon executable and waits it finishes or crash. when it do, restart it. Then you don't have to pay attention for running twice your code, and you can end gaming with cron, because cron isn't for that work.

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.