July 24th, 2011, 02:44 PM
Crontab help - prevent duplicate process
Hi. I know how to set crontab to make a program run but is there a way or additional syntax/code I can add to the line of my cron job that whenever it runs the program it will check whether there's a process already running?
July 24th, 2011, 03:49 PM
Have your cron job launch a shell script which checks if the process is running. If it is running, your shell script just exits without doing anything. If it is not running, your shell script would launch it.
There are various ways to test if a process is running. A most common one is to record the PID when launching the process in a file somewhere. Your shell script can then test if that file exists, and if so check the the process specified by that pid is still running using the ps command.
Recycle your old CD's, don't just trash them
If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
July 24th, 2011, 04:20 PM
That seems a bit hard for me as I don't do shell script. I just know how to set up the line on crontab.
But I found this and it seems using flock. It doesn't explain much though. I'm wondering if it means when the script runs it creates a file which has a lock and will auto remove once it's done.
So if the next job starts and see there's a lock then it won't lock again which means this next job will stop?
Is this correct?
July 24th, 2011, 04:42 PM
Hi. I just found this and it seems pretty good. My script is php.
Here's the link for those one who's interested.