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).


4 Answers 4


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.

  • For 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.
    – webnoob
    Feb 19, 2011 at 22:32
  • @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. Feb 19, 2011 at 22:52
  • I do own the script, it is one I have written. I will look at the flock function, thanks.
    – webnoob
    Feb 20, 2011 at 0:48
  • @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?
    – webnoob
    Feb 20, 2011 at 2:09
  • 1
    @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. Feb 20, 2011 at 2:52

Use a bash script as your cron job



if [ ! -e $lockfile ]; then
   touch $lockfile
   php /path/to/php/script.php
   rm $lockfile
   echo "script already running"
  • 1
    Hi 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
    Feb 20, 2011 at 0:48
  • You 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 uses kill -9 for example. Feb 20, 2011 at 0:54
  • I have the same questions as webnoob had. Could you please answer?
    – d a i s y
    Jun 11, 2018 at 9:31

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.


* * * * * solo -port=3801 ./job.pl blah blah

I am curious if implementing something with ps will solve the issue. Would simple ps -e | grep script_name.php | awk '{print $4}' be sufficient? It definitely will tell you if the script is running, no need to check if someone killed it beforehand. How creating and then removing a lock file is better/faster than this?

I am not sure, just want to know pros and cons. Thanks

  • A lock file may be more crossplatform since it only involves to create a file while checking for the running process relies on specific tools. Mar 18, 2012 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .