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.

Won Jun Bae
  • 5,140
  • 6
  • 43
  • 49
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
  • 4
    possible duplicate of [Run php script as daemon process](http://stackoverflow.com/questions/2036654/run-php-script-as-daemon-process) – Pekka Jan 17 '11 at 19:50
  • 2
    Lots of good info: http://stackoverflow.com/search?q=php+daemon – Pekka Jan 17 '11 at 19:50
  • 1
    check "screen" utility as well to detach your session from ssh (and reconnect it later if you want) – regilero Jan 17 '11 at 19:58
  • 1
    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) :-) – James Butler Jan 17 '11 at 21:14

7 Answers7

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.

Community
  • 1
  • 1
ActionOwl
  • 1,473
  • 2
  • 15
  • 20
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.

KDrewiske
  • 1,539
  • 1
  • 16
  • 17
  • This would be my first choice as well. – laketuna Jan 18 '18 at 15:51
  • 1
    hi @KDrewiske can you provide any code sample or something which can help peoples who are looking for the same issue. I am one of them :) – Pankaj Jha Apr 05 '18 at 19:45
  • 1
    @PankajJha - I just searched for "ubuntu upstart example" and got a nice guide from Digitial Ocean. https://www.digitalocean.com/community/tutorials/the-upstart-event-system-what-it-is-and-how-to-use-it. 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. – KDrewiske Apr 13 '18 at 00:41
  • @KDrewiske 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](http://blog.bobbyallen.me/2014/06/02/how-to-create-a-php-linux-daemon-service/) I hope this will help someone, just like it helped me. – Pankaj Jha Apr 17 '18 at 14:50
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.

legoblocks
  • 525
  • 1
  • 6
  • 16
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

Community
  • 1
  • 1
Christian Joudrey
  • 3,441
  • 25
  • 25
2

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

AntonioCS
  • 8,335
  • 18
  • 63
  • 92
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.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
-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.

posixpascal
  • 3,031
  • 3
  • 25
  • 44
  • 2
    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. – Tore Feb 18 '16 at 22:21