Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '11 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. –  Dennis Williamson Feb 19 '11 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 '11 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 '11 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. –  Dennis Williamson Feb 20 '11 at 2:52
show 1 more comment

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

share|improve this answer
    
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. –  Boris Guéry Mar 18 '12 at 13:42
add comment

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
share|improve this answer
    
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 '11 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. –  Dennis Williamson Feb 20 '11 at 0:54
add comment

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.