Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Preventing collision in Cron - File lock safe?

I'm trying to find a safe way to prevent a cron job collision (ie. prevent it from running if another instance is already running).

Some options I've found recommend using a lock on a file.

Is that really a safe option? What would happen if the script dies for example? Will the lock remain?

Are there other ways of doing this?

like image 368
Ben Avatar asked Mar 25 '11 04:03

Ben


2 Answers

This sample was taken at http://php.net/flock and changed a little and this is a correct way to do what you want:

$fp = fopen("/path/to/lock/file", "w+");
if (flock($fp, LOCK_EX | LOCK_NB)) { // do an exclusive lock
  // do the work
  flock($fp, LOCK_UN); // release the lock
} else {
  echo "Couldn't get the lock!";
}
fclose($fp);

Do not use locations such as /tmp or /var/tmp as they could be cleaned up at any time by your system, thus messing with your lock as per the docs:

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html

Do use a location that is under your control.

Credits:

  • Michaël Perrin - for proposing to use w+ instead of r+
like image 120
zerkms Avatar answered Nov 08 '22 11:11

zerkms


In Symfony Framework you could use the lock component symfony/lock

https://symfony.com/doc/current/console/lockable_trait.html

like image 29
Sebastian Viereck Avatar answered Nov 08 '22 09:11

Sebastian Viereck