How to check whether a php script?

Run the script to screen loop(-1). But it can be completed with the words "killed" (Limit vds)
I need another script would check if it works if it works - do nothing, otherwise start it again in the same screen.
1. Tried:
$fh = fopen(__FILE__, 'r');
 if (!flock($fh, LOCK_EX | for lock_nb)) {
 die('Script is already running!');

run the script in the screenshot, left it, tried again to run - out "'Script is already running!". Like throw it in cron and all the rules, but... Come back in a screen, finished the script (Ctrl + z) - launch again and the same inscription: "Script is already running!". What is the trouble?
2. Tried to save the pid and check if it works:
if (posix_kill($pid, 0))
 echo "Process is running.";

In the end though the script is running or not, still writes that "the process started".
July 2nd 19 at 13:13
4 answers
July 2nd 19 at 13:15
Try to create .lock file on startup and delete when completed and check for other scripts. If the file is there then the script is either running or aborted.
Well, if the file is -> on or off it crashes, then what)) - Seamus_Upton commented on July 2nd 19 at 13:18
: I initially misunderstood your question. You want 1 the script 2 run script, if he accidentally died? In this case, your architecture is fundamentally wrong. PHP created to die, i.e. to run the script, work it and finish. Why do you need a continuously running script? - mohamed.Muller commented on July 2nd 19 at 13:21
Well the task is completed) the Script runs in an infinite loop waiting for data, executes them. - Seamus_Upton commented on July 2nd 19 at 13:24
: in my opinion this will be an startup script for Cron, you can't do this? - mohamed.Muller commented on July 2nd 19 at 13:27
July 2nd 19 at 13:17
You can hang it on monit
July 2nd 19 at 13:19
July 2nd 19 at 13:21
There is such a thing as a supervisor(ctl).And the screen does not need to be,and the crutch of raising the built-in.

Find more questions by tags PHPLinuxCronScreenshots