How to keep a persistent PHP script running?
I have a PHP script that listens for incoming socket requests, etc. I need this script to be continually running (it runs within an infinite loop) on the server.
How can I initiate and manage this process? I tried just starting it up through SSH/putty but as soon as the SSH connection times out the开发者_Go百科 script dies.
myscript.php &
This will run the scriptin the background
you can check it with
ps aux | grep myscript.php
As Patrick has mentioned in the comments below, there is no maximum execution time for PHP scripts run from command line. myscript.php
will run indefinitely.
We recently had a similar need. We are running an Ubuntu 14.04 LTS server, so created an upstart
process to start the process at boot as and restarts if it exits for any reason.
It's worked out extremely well for us, including the automatic restart; our long-running process eventually looses the MySQL connection. The script handles the exception and exits, before the upstart
process automatically restarts the process. We have also added a cron
script to monitor the process, just in case there is a bigger error that upstart
gives up trying to restart; we have a fail-safe in the event a simple restart doesn't correct the prior error.
If long-running reliability is the goal then, supervisord is perfect for this!
I came across it via https://lornajane.net/posts/2012/watch-over-long-running-processes-with-supervisord
Edit: This tool's purpose is to solve the stated problem. The stated goal is to keep the long-running process running. A comment pointed out (validly) that the currently accepted solution will not be reliable, as the script will likely crash etc. So the solution is to use a tool to "initiate and manage this process", that also keeps it alive. This tool does exactly that: it starts the process in the background and keeps it running.
Run the script in the background through SSH as explained here: Getting ssh to execute a command in the background on target machine
I would then suggest you have a way to monitor if the script is still running. You mentioned your script listens on a port, you could maybe write a script that checks every once in a while to see if the port is still open.
Another option is to monitor the process id. When the script first executes, you can grab the process id using getmypid
function and store it in a file. You can then periodically check if it is still running using ps -p 1234
.
Another solution: How to check if a php script is still running
You can use the browser if you use ignore_user_abort() and set_time_limit()
You can run it command line. You will have to add a "run in the background" modifier in order to make it so the system doesn't wait for the script to complete. Basically you put an & at the end of your command line.
One thing you should definitely do is create an ini shell script that runs the PHP script on bootup incase of any restarts.
Why not a do while loop? do { /* your code */ } while $something == 0;
Then when something happens to trigger the halt of the script, set $something to a value other than 0.
精彩评论