Is there any way how to ensure some process will be always running?
Let’s say i need this to be running:
php -f myscript.php -param1=value1
Now i do it this way:
The proccess is launched and right after that the PID is written to the file (myProcess.pid).
Then i schedule a cronjob, which is periodically trying to launch the process again and again each 5 minutes.
The process is actually a bash script file, which firstly checks, whether myProcess.pid exists and whether is, if the PID in that file is really running, if it’s not, it launches it and rewrite the “myProcess.pid” with new PID.
There are several problems with that solution:
- What about the “blackout” time period between “cron checks”?
- What about the period? Isn’t 5 minutes too much to spend unncessary CPU time?
- What if the file myProcess.pid has been compromised, or just deleted by someone/something? Then the bash script launches it again, even if it’s already running.
- What if the process dies and the exact PID takes another process?
Does any better approach exist?
You’ll need some kind of monitoring application like
Alternatively, you can create some daemonized php code: