unix - executing one php script at a time from a script list -
i have web application user can choose list of scripts execute , executions added table in mysql , each 1 have own state "pending,"success" ,"failed" or "in progress" user can choose stop execution.
the problem 1 script can executed @ same time others have wait until finished.
my environement linux (ubuntu) , scripts in php though doing crontab executes php script , php script grab informations sql table , search if there other execution looking if there there execution "in progress" state if there 1 exit,otherwise execute other execution having pending state. there other solution ?
it's better use atomic check. way how database not atomic after checked no other scripts running, before you've written current script starts, process may perform same check , therefore you'll 2 concurrent scripts running.
also if script terminates abnormally reason, won't update database, other scripts won't able start @ all.
more reliable way use file locking:
$lock_file = 'some_path/process.lock'; $fd = fopen($lock_file, "w"); if (!$fd) throw new exception("can't open file, check permissions on ".$lock_file, 1); if (!flock($fd, lock_ex + lock_nb)) throw new alreadyrunningexception("can't lock file - script running", 0);
then, after script job done, unlock file:
flock($fd, lock_un); fclose($fd);