Answer the question
In order to leave comments, you need to log in
How to create an instantly self-restarting php script?
To run the script in the debian console, I enter
php /some_path/some_file.php
The script runs for some time, and ends, it may end as a result of some kind of error (for example, the connection to the database is lost, or some other). How can I make it restart immediately? And so that in a unit of time there would not be two scripts running /some_path/some_file.php at the same time? Can this be done with some debian console commands?
Answer the question
In order to leave comments, you need to log in
I would advise using a specialized process manager. For example supervisor
Instruction in Russian
while(true);do php some.php;sleep 1;done
sleep 1 is to have a pause between launches, you can remove it, but if an error occurs immediately at startup, such a script will eat resources just like that, which sometimes costs a pretty penny in cloud hosting with payment for resources.
do not forget to mark the error in the log to respond to them
In fact, the right question is " why create a self-restarting PHP script".
PHP is built to die .
I made daemons in PHP that worked for months, but initially, yes, there were problems, for example, I had a queue to monitor the script, and as soon as I caught an incoming message, it started processing, a timeout in mysql worked, I had to write a function to check the connection, and if it doesn’t exist, then reconnect, this function was only launched at the beginning of one processing ...
All sorts of things like infinite loops, you need to delay at least 500 ms ...
In general, you can write a script on bash that rattles the PID in the output of ps -aux and if not such a process, then run it, and at the first start save it to a file of some type> script.pid...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question