Don't run cron job if already running
Solution 1
Please see Process Management. You will probably want to use a lock file. You can use flock
for example.
Why can't you let your jobs complete?
Your questions says "I either didn't understand the answer or it didn't apply to me", but you don't say in what way. Please be more specific.
Solution 2
Use a bash script as your cron job
cron.sh
#!/bin/bash
$lockfile=/tmp/lock.file
if [ ! -e $lockfile ]; then
touch $lockfile
php /path/to/php/script.php
rm $lockfile
else
echo "script already running"
fi
Solution 3
Docs: https://www.timkay.com/solo/
solo is a very simple script (10 lines) that prevents a program from running more than one copy at a time. It is useful with cron to make sure that a job doesn't run before a previous one has finished.
Example
* * * * * solo -port=3801 ./job.pl blah blah
Related videos on Youtube
webnoob
I am Cabinet Maker turned programmer - Odd but true :) I have a passion for all things coding including C#, PHP, CSS, JavaScript. I'm currently building a product that will help people manage their service based business including job management, accounts tracking, scheduling and planning etc. It's an exciting project.
Updated on September 17, 2022Comments
-
webnoob over 1 year
I know this question has been asked already but I either didn't understand the answer or it didn't apply to me.
I have a php script that I am calling every 1 minute using CPanel to set up the Cron Job. The nature of the script means that it could overrun for just over the minute so I need to know how to stop the next one running if the first one hasn't completed.
I have a VPS running CENTOS 5.5 and have access to WHM and CPanel. I have never used Linux before (only just got the server yesterday) so I have no idea what I am doing and would appreciate some help if possible. If I need to provide more information please let me know (I don't know what info you would need at the moment).
Thanks.
-
webnoob over 13 yearsFor instance, someone has posted a shell script, but I wouldn't know what to do with it or how to modify it to make it work for me. The jobs can't complete because it involves waiting for 10 seconds to request some files again and then processing them. Although I am calculating how much time this took, its not exact. So there may be some over spill.
-
Dennis Williamson over 13 years@webnoob: Is the PHP script your own that you can modify? Can you implement a lock file in it and if a subsequent run sees the lock file it exits? When the first instance exits it should remove the lock file. If it runs and doesn't see a lock file it should continue on to do its processing. PHP has a flock function.
-
webnoob over 13 yearsHi Pablo, Would you be able to explain that for me please? Where would I put the file? Do I need to do anythign with my php script?
-
webnoob over 13 yearsI do own the script, it is one I have written. I will look at the flock function, thanks.
-
Dennis Williamson over 13 yearsYou need to use a
trap
in the Bash script to remove the lock file in case the script is interrupted. Stale locks can still be a problem if someone useskill -9
for example. -
webnoob over 13 years@Dennis: Question - If I was to use the flock function, and the file was locked at the second minute when the cron was due to run. Would the cron job queue itself, or would it wait until minute 3 which would be the next scheduled time?
-
Dennis Williamson over 13 years@webnoob: It depends on how you do it exactly, but the job would be started by cron, the script would check for a lock, if the lock is there it would exit, if not then it would create the lock and continue to do its task. When it finishes, it removes the lock. The cycle would repeat at the interval specified in the cron entry. If the script waited for the lock to clear rather than exiting, you could conceivably have many jobs queued waiting which I doubt that you'd want. There are some circumstances where you'd want to wait, but I think exiting suits your needs.
-
webnoob over 13 yearsOk, thanks for all your help. I think that answers my questions in detail.
-
Boris Guéry about 12 yearsA lock file may be more crossplatform since it only involves to create a file while checking for the running process relies on specific tools.
-
d a i s y about 6 yearsI have the same questions as webnoob had. Could you please answer?