I have seen a PHP code that uses shell commands to determinate if this process is running now, but I’ve lost it. Can you give me a hint how to do this?
The idea is to create a php file and run this file as a cron job using php -f. The execution time of the script may be 10 seconds, but it also may be 10 minutes. I need to make this cron run once every minute and if the cron from the prevous minute is still running – to stop the new one and let the other one finish.
I’m not very good in shell programming so I need some help.
P.S The idea is to create a daemon without putting an endless PHP process in background.
for many purposes, the following code is ok (there may be a race condition, but for a minutely cronjob this is probably very rare):
The global variable
$lock_takenmay be useful to make sure that the lockfile is removed whenever execution is halted. The$lockstringcould be a date or a Unix timestamp, which may be useful for removing the lock if it is clearly too old (butfilemtime()could be enough).If you absolutely cannot risk the race condition, you have to do something like
This only works for local lockfiles, though.