i'm using cronjob run php script executed every 1 minute
i need make sure of copy running if php script still running after 2 minutes, cronjob should not run version.
currently have 2 options , see feedback , if have more options
option 1: create tmp file when php script start , remove when php script finish (and check if file exists) ---> problem me option if have php script crash reason, not run again (the tmp file not deleted)
option 2: run bash script 1 below control php script execution ---> looking can done within php
#!/bin/bash function rerun { basedir=$(dirname $0) echo $basedir/$1 if ps -ef | grep -v grep | grep $1; echo "running" exit 0 else echo "not running"; /usr/local/bin/php $basedir/$1 & exit $? fi } rerun myphpscript.php
ps: saw "mutex class" @ http://www.php.net/manual/en/class.mutex.php not sure if it's stable , tried it.
you might want use library ninja-mutex provides simple interface handling mutex. can use flock, memcache, redis or mysql handle lock.
below example uses memcache:
<?php require 'vendor/autoload.php'; use ninjamutex\lock\memcachelock; use ninjamutex\mutex; $memcache = new memcache(); $memcache->connect('127.0.0.1', 11211); $lock = new memcachelock($memcache); $mutex = new mutex('very-critical-stuff', $lock); if ($mutex->acquirelock(1000)) { // critical stuff // , release lock after finish $mutex->releaselock(); } else { throw new exception('unable gain lock!'); }
Comments
Post a Comment