Jump to content

Check out our Community Blogs

- - - - -

Running PHP script as daemon / system service / on reboot

Posted by Upstream, in nohup php, Ubuntu init, bash, PHP, Uncategorized 09 July 2015 · 12274 views

php api twitter bash init ubuntu service
Running PHP script as daemon / system service / on reboot Hi folks,

Done something cool today, at least for me it's cool as its very handy. I desided to share it here as I don't come accross much info on these when I was looking for it myself. So here I go. The below bash script runs as a system service at start up on Ubuntu and all it does is start a background process with a easy to lookup process id (pid) and then runs a simple shell script that just does one thing: launch a php daemon with the command nohup php myscript.php. With only 2 edits we can run any php file as a daemon and that's really handy as they run at boot and can be called with service phpscript start/stop/reload.

Posted Image

I use this skeleton to run all my php daemons for my custom Twitter application for businesses. For example one of several of my php daemon scans for tweets (realtime) containing keywords specified by me and then send the tweets + lots of extra info via this daemon. PHP is often used this way in API programming. In example the Database Server for Twitter from dev140.com that I uses nohup php daemon.php to create the persistent connection to the api this way. It's really important that these run all the time as it sends tweets real-time when the are tweeted.
When running an application that is connected to an API like the Twitter streaming API (Firehose) we need to have a persistent connection with the Twitter API. Persistent
means connecting once and staying connected 24/7 and this 365 days a year. Or untill we need to reboot of course. Most API programming is done in PHP or something similar that doesn't involve much SSL or console operations as root on the web server so that is not something all API programmers have done much. Most would rather leave it that way, just like me some years ago because it involves new things like UNIX shell scripting (bash i.e.). But we need a program running 24/7 persistently connected to the API. So what to do?

Use PHP scripts as system scripts running as daemon and be a happy API programmer!

With this script we can run any PHP file as a daemon from boot as root and as a service and all we need to do is change the the program file name (php script) and its location (use absolute path only).

Connect to your server via SSL as root because only as root we can use get_tweets start/stop/reload for the seperate daemons. On Ubuntu we need to do the following to make a script into a daemon this way and also run them on system reboot.

1) create /etc/init.d/get_tweets
2) place code below in /etc/init.d/get_tweets (nano /etc/init.d/get_tweets)
3) sudo chmod +x /etc/init.d/get_tweets
4) update-rc.d /etc/init.d/get_tweets defaults 98 02
5) add line to rc2.d: /etc/init.d/update-rc.d /etc/init.d/get_tweets
5) add line to rc.local: /etc/init.d/update-rc.d /etc/init.d/get_tweets
6) reboot

Pay attention: don't place the files run as root in the folder /public_html or any subfolders of the folder. For example place the /dev140 folder somewhere in the same folder next to /public_html like /home/someuser/domains/example.com/dev140 and use this path for the PROG_PATH var and as the base url for this file.
# Provides: get_tweets
# Required-Start: 2 3 4 5
# Required-Stop: 0 1 6
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PHP get_tweets.php nohup daemons initscript
# Description: This file should be placed in /etc / init d

# Fill in name of php daemon file and run as System Daemon /

## If not already running start php daemon
start() {
    if [ -e "$PID_PATH/nohup php $PROG.pid" ]; then
        ## Program is running, exit with error.
        echo "Error! $PROG is currently running!" 1>&2
        exit 1
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.

        cd $PROG_PATH
        nohup php $PROG_PATH/$PROG > /dev/null &

        echo "nohup php $PROG_PATH/$PROG started as system service"
        touch "$PID_PATH/nohup php $PROG.pid"


## If runinng kill php daemon
stop() {
    if [ -e "$PID_PATH/nohup php $PROG.pid" ]; then
        ## Program is running, so stop it

       kill $(ps aux | grep "php $PROG_PATH/$PROG" | awk '{print $2}')

        rm "$PID_PATH/nohup php $PROG.pid"

        echo "System service $PROG stopped"

        ## Program is not running as system service, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1

        case "$1" in
        exit 0
        exit 0
        exit 0
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
==== tested on ====

No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.2 LTS Release: 14.04 Codename: trusty

  • 2

edit: step 5 was not correct. Copy paste error or human error of mine. One day programmers will become obsolete as they make errors and programs don't :D

    • 1

January 2017

151617 18 192021

My Picture