How to keep a persistent PHP script running?
Asked Answered
S

7

12

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 script dies.

Sussi answered 17/1, 2011 at 19:47 Comment(4)
possible duplicate of Run php script as daemon processSupersaturated
Lots of good info: stackoverflow.com/search?q=php+daemonSupersaturated
check "screen" utility as well to detach your session from ssh (and reconnect it later if you want)Oxford
If you have a long running php script as a solution, you have either have an academic problem, a strange set of circumstances, or on a road to a better solution when you get your head around it. Back on topic, at an abstract level you want something to manage your php process to restart it when dies, goes oom, the moon shifts alignment etc. (this manager should't be another php script) :-)Templin
A
7
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.

Astroid answered 17/1, 2011 at 19:54 Comment(3)
You can override it with set_time_limit.Hylozoism
This isnt really stable since script can die of error or memory leak and not start up again.Pneumatometer
This is work for me, But after reboot server I need to set manually is there any way to set after rebooting server script start automatically?Slapup
C
6

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.

Contrapuntist answered 6/4, 2017 at 16:14 Comment(4)
This would be my first choice as well.Viradis
hi @Contrapuntist can you provide any code sample or something which can help peoples who are looking for the same issue. I am one of them :)Cloyd
@PankajJha - I just searched for "ubuntu upstart example" and got a nice guide from Digitial Ocean. digitalocean.com/community/tutorials/…. It discusses much more than what my example could provide. The PHP script that it executes is just something that could be called via CLI - simply containing a code in a while-loop.Contrapuntist
@Contrapuntist really appreciate your help, thanks, and this is exactly what I needed. I also looked up few more tutorials like this and the best one I found was This Link I hope this will help someone, just like it helped me.Cloyd
P
3

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.

Polecat answered 25/8, 2018 at 23:58 Comment(0)
H
2

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

Hylozoism answered 17/1, 2011 at 19:49 Comment(0)
D
2

You can use the browser if you use ignore_user_abort() and set_time_limit()

Dibranchiate answered 17/1, 2011 at 20:32 Comment(1)
doc refers ignore_user_abort() to commandline instead of browerElamite
B
0

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.

Brede answered 17/1, 2011 at 19:52 Comment(0)
C
-3

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.

Certitude answered 17/1, 2011 at 19:50 Comment(1)
I believe that $something = 0; is an assignment and not a true/false test .. ;) But I'm sure you knew that .. Another thing is that if you base anything on a loop scenario, unless your logic ( script / function ... whatever ) blocks on something - any loops while 'idle' wont be idle at all, but straining the server cpu wasting precious resources on a server-wide or at least virtual server-wide scope.Vorster

© 2022 - 2024 — McMap. All rights reserved.