Run a PHP script as a service/daemon using start-stop-daemon

In a recent project I needed to permanently run a PHP CLI script; it needed to run as a service/daemon. A few Google search coupons later I got my answer: use start-stop-daemon to make it work.

To not manually type in those lengthy commands it’s possible to wrap these in a handy little script, as shown in the gist embedded below:

“Installation” is straightforward: save the file into /etc/init.d/myservice and make it executable by running chmod +x /etc/init.d/myservice. Change NAME, DESCRIPTION, DAEMON, and DAEMON_OPTS to reflect your setup.

Once done you can start the service by running /etc/init.d/myservice start and stop it by running /etc/init.d/myservice stop. Restarting is also possible by running /etc/init.d/myservice restart.

Verify the running of the daemon by checking the contents of the created PID file: cat /var/run/myservice.pid — It should contain a process ID.

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Join the Conversation

8 Comments

  1. Thank you very much! Yes, I read the “PHP is meant to die” post first. Both of them. However, I can make any test that will fail with any language (as many of the people mentioned). My biggest problem with PHP running continuously has been timing when there are too many inputs and outputs (I’m answering phone calls and distributing those calls using web sockets). I’m going to move to Node.js for that process (although I may look at React now). However, for everything else I’m doing with that site PHP works perfectly. I’ve got about five or six processes that do things like send and resend emails/texts/faxes, create those aforementioned emails when necessary, and do lots of other things. These processes work day after day, week after week, without a problem (except when my user’s hardware or network has a problem). Am I sending 20mb at a time? Not a chance. This is a real world application, not a test meant to fail.

    Can I write programs as quickly in C or C# or (barf) Java? No. Is there the community and support for other languages like there is for PHP? Nothing I’ve ever seen. And, I’ve written in everything from assembly to Basic to Delphi (Pascal) to C,C# and now to PHP and Javascript. I’ll stick with those last two, thanks. I’ve dealt with products that use Java (barf again) and Erlang (give me a break). I admire those willing to hassle through C and C++. I don’t have enough years remaining.

    But, I want to thank you again for this bash script. I suspect my power is going to go down today and I have wanted to be able to run a PHP script as a service for quite some time. Your code let me do it. And, as I said earlier, one of the major reasons I wish PHP were around 30 years ago! The support and community built around this language is second to none.

    1. Hahaha, I just had to smile at the (barf) Java! hehe, good one 😉

      PS: Also going to make use of this awesome bash script.

  2. Ok well I’m getting this error in centOS

    Starting Daemon for my magnificent PHP CLI script: /etc/init.d/message_queue: line 33: start-stop-daemon: command not found

  3. Bramus,
    I’m using your script and it works like charm when I run it in shell. But it doesnt work when I execute it in PHP with following script:

    $cmd = exec(“/etc/init.d/myservice start”);

    Do you have any idea?

    Thx

  4. this is my skript in /etc/init.d/
    #! /bin/sh

    # Installation
    # – Move this to /etc/init.d/myservice
    # – chmod +x this
    #
    # Starting and stopping
    # – Start: `service myservice start` or `/etc/init.d/myservice start`
    # – Stop: `service myservice stop` or `/etc/init.d/myservice stop`

    #ref http://till.klampaeckel.de/blog/archives/94-start-stop-daemon,-Gearman-and-a- little-PHP.html
    #ref http://unix.stackexchange.com/questions/85033/use-start-stop-daemon-for-a-php- server/85570#85570
    #ref http://serverfault.com/questions/229759/launching-a-php-daemon-from-an-lsb-init- script-w-start-stop-daemon

    NAME=canio231
    DESC=”canio daemon 231″
    PIDFILE=”/var/run/${NAME}.pid”
    LOGFILE=”/var/log/${NAME}.log”

    DAEMON=”/usr/bin/php”
    DAEMON_OPTS=”S7/getdata_CGI.php dsn=6 modus=simulation runtime=600 ip=192.168.41.231 config=db”
    START_OPTS=”–start –chdir /usr/share/bbkappli/CANio/htdocs –background –make-pidfile –pidfile ${PIDFILE} –exec ${DAEMON} ${DAEMON_OPTS} 1>>/var/log/ca$

    STOP_OPTS=”–stop –pidfile ${PIDFILE}”

    test -x $DAEMON || exit 0

    set -e

    case “$1” in
    start)
    echo -n “Starting ${DESC}: ”
    start-stop-daemon $START_OPTS >> $LOGFILE 2>&1 &
    echo “$NAME.”
    ;;
    stop)
    echo -n “Stopping $DESC: ”
    start-stop-daemon $STOP_OPTS
    echo “$NAME.”
    rm -f $PIDFILE
    ;;
    restart|force-reload)
    echo -n “Restarting $DESC: ”
    start-stop-daemon $STOP_OPTS
    sleep 1
    start-stop-daemon $START_OPTS >> $LOGFILE
    echo “$NAME.”
    ;;
    *)
    N=/etc/init.d/$NAME
    echo “Usage: $N {start|stop|restart|force-reload}” >&2
    exit 1
    ;;
    esac

    WHY MY SCRIPT IS NOT LOGGING ?

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.