Good day.

I run the script with a crown at intervals of 5 minutes. Is it possible to determine whether the script is still running or not?

Just if the script is already executed, it is not advisable to launch it with a double ..

2 answers 2

One of the test options is to use the system file lock. In this case, even if the process dies, the lock will be released and the subsequent launch will not be locked.

function lock($name) { $lock = sys_get_temp_dir()."/$name.lock"; $aborted = file_exists($lock) ? filemtime($lock) : null; $fp = fopen($lock, 'w'); if (!flock($fp, LOCK_EX|LOCK_NB)) { // заблокировать файл не удалось, значит запущена копия скрипта return false; } // получили блокировку файла // если файл уже существовал значит предыдущий запуск кто-то прибил извне if ($aborted) { error_log(sprintf("Запуск скрипта %s был завершен аварийно %s", $name, date('c', $aborted))); } // снятие блокировки по окончанию работы // если этот callback, не будет выполнен, то блокировка // все равно будет снята ядром, но файл останется register_shutdown_function(function() use ($fp, $lock) { flock($fp, LOCK_UN); fclose($fp); unlink($lock); }); return true; } 

Or you can store it in the process PID file, as suggested by @avp . The difference of these approaches will be visible only on Windows.

    You can at the time of the script to create a file, file a flag - if there is a file, then we do not work.
    And at the end of the cron script, delete the file.

    • In the file (for reliability) you can put and check your PID (script from the crown), if such a process is running, then the file is relevant. - avp
    • There is a nuance, if the cron dies, the file will remain, but the process will not be - Node_pro