[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi,

PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime.

I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file.

However, I'd like to make sure that only one instance of the cron script is running. It would also be useful for me to be a 100% certain that the cron script is actually running when it tells me it is.

Can it be done? If yes, how? I have unfortunately no experience in this area.

Regards,

-jj. :slight_smile:

Your script has a log file, right? Have it write to the log file after it's finished doing whatever it does, and before it sleeps:

echo 'date' Done - going to sleep >> /tmp/script.sleep.log

You can then tail -f the log and watch it roll, check the timestamp on it to see when last updated, figure out when it failed, etc.

I usually count processes by doing something like this:

ps -aef | grep -v grep | grep very_unique_scriptname | wc -l | bc

Thank you very much. :slight_smile:

I'm very new to this, so please excuse a fire of questions.

I hadn't thought about a log file. But it makes sense. However, since the script executes itself every 30 seconds, 8 hours a day, 5 days a week, I guess I would only log failures (if not, I would end up with a huge log file). What would be your approach?

The tail command reads and print the final lines of an input. So I would basically read the log file, correct?

Very interesting, even though it will take some serious time for a command newbie like me to translate it into an understandable sentence :slight_smile: