Search code examples
linuxshellunixpidkill-process

How to restart background php process? (how to get pid)


I'm a PHP developer, and know very little about shell scripting... So I appreciate any help here.

I have four php scripts that I need running in the background on my server. I can launch them just fine - they work just fine - and I can kill them by looking up their PID.

The problem is I need my script to, from time to time, kill the processes and restart them, as they maintain long standing HTTP requests that sometimes are ended by the other side.

But I don't know how to write a command that'll find these processes and kill them without looking up the PID manually.

We'll start with one launch command :

/usr/local/php5/bin/php -f /home/path/to/php_script.php > /dev/null &

Is there a way to "assign" a PID so it's always the same? or give the process a name? and how would I go about writing that new command?

Thank you!


Solution

  • Nope, you can't "assign" the process PID; instead, you should do as "real" daemons do: make your script save its own PID in some file, and then read it from that file when you need to kill.

    Alternative would be to use something like supervisor, that handles all that for you in a quite nice way.

    Update - supervisor configuration

    Since I mentioned supervisor, I'm also posting here a short supervisor configuration file that should do the job.

    [program:yourscriptname]
    command=/usr/local/php5/bin/php -f /home/path/to/php_script.php
    

    Have a look here for more configuration options.

    Then you can use it like this:

    # supervisorctl status
    

    to show the process(es) status.

    # supervisorctl start yourscriptname
    

    to start your script

    # supervisorctl stop yourscriptname
    

    to stop your script

    Update - real world supervisor configuration example

    First of all, make sure you have this in your /etc/supervisor/supervisord.conf.

    [include]
    files = /etc/supervisor/conf.d/*.conf
    

    if not, just add those two lines and

    mkdir /etc/supervisor/conf.d/
    

    Then, create a configurtion file for each process you want to launch:

    /etc/supervisor/conf.d/script1.conf

    [program:script1]
    command=/usr/local/php5/bin/php -f /home/path/to/php_script.php
    stdout_logfile=/var/log/script1.log
    stderr_logfile=/var/log/script1-error.log
    

    /etc/supervisor/conf.d/script2.conf

    [program:script2]
    command=/usr/local/php5/bin/php -f /home/path/to/php_script2.php
    stdout_logfile=/var/log/script2.log
    stderr_logfile=/var/log/script2-error.log
    

    ...etc, etc.. for all your scripts.

    (note that you don't need the trailing & as supervisor will handle all the daemonization thing for you; in fact you shouldn't execute programs that are self-daemonizing inside supervisor).

    Then you can start 'em all with:

    supervisorctl start all
    

    or just one with something like:

    supervisorctl start script1
    

    Starting supervisor from php

    Of course, you can start/stop the supervisor-controlled processes using the two commands above, even from inside a script.

    Remember however that you'll need root privileges, and it's quite risky to allow eg. a web page to execute commands as root on the server..

    If that's the case, I recommend you have a look at the instructions on how to run supervisor as a normal user (I never did that, but you should be able to run it as the www-data user too..).