Is there a way I can automatically restart a PHP script whenever it exits, regardless of whether it has been exited properly, or has terminated due to an error, or maxed memory usage, etc.?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A PHP Script can also restart itself with PCNTL.
Disclaimer: the point of this exercise is only to prove that PHP is perfectly capable of restarting itself and to answer the question:
It is therefor beyond our scope to go into any detail about unix processes so I would suggest you start with the PCNTL book or refer to php-pcntl for more details. Developer discretion is required, just because we can, doesn’t mean it’s sensible. With all seriousness aside lets have some fun.
In the examples we will assume it is a PHP CLI script launched in a *nix environment from a terminal with a semi-decent shell using the command:
We pass a restart count attribute as indicator that the process was restarted.
Can I automatically restart a PHP Script?
Yes I can!
Regardless of whether it has been terminated properly?
Yes I can restart if terminated!
Or terminated due to an error?
Ditto!
But you know I am going to want more than that right?
Certainly! I can restart on signals kill, hub, and even Ctrl-C!
How do I terminate it now?
I don’t want to see all these errors can I restart on those too?
No problem I can handle errors too!
Although this appears to be working fine at first glance, because
pcntl_execruns in the same process we do not notice that things are terribly wrong. If you wanted to spawn a new process and letting the old one die instead, which is a perfectly viable alternative see next post, you will find 2 processes are started each iteration, because we trigger a PHP NOTICE and an ERROR due to a common oversight. This can easily be rectified by ensuring wedie()orexit()after the call topcntl_exec, since implementing an error handler PHP assumes tolerance were accepted and continues running. Narrowing the error reporting level instead of catchingE_ALLwill be more responsible as well.The point I am trying to make is that even if you are able to relaunch a script that failed this is not an excuse to allow broken code. Although there may exist viable use cases for this practice, I strongly disagree that restart on failure should be employed as a solution for scripts failing due to errors! As we can see from these examples there is no way of knowing exactly where it failed so we cant be sure where it will start from again. Going for the "quick fix" solution which could appear to be working fine may have more problems now then before.
I would instead prefer to see the defect addressed with some proper unit tests which will help flush out the culprit so we may rectify the problem. If PHP runs out of memory it can be avoided through conservative use of resources by unsetting the variables after use. (btw. you will find assigning null is much quicker than using unset for the same result) I am afraid though, if left unresolved, restarting would most definitely serve as suitable opener to the can of worms you already have.