1

Till now I had no real need to learn bash and init scripts as I need to at this point. I have created a custom Twitter app that is persistently, or constantly, connected to the API. It does tasks like downloading followers profiles, statuses etc. Cron jobs don't help much for this so I progressed to use the skeleton of the dev140 developers PHP daemon class for the Phirehose API (persitent connection streaming api). So I now mimic a bit of persistent connection state by having a daemon do the things that are actually singe api calls on the normal (non streaming api) that otherwise are done with cron jobs. The daemon just reads from a que table in database and if its not emty it know what to to with it. It are ids and a job type and then it goes of and fetch what I need when I need it with the max. bandwidth I can get within a 15 min frame window. Much much more steady this way.

I made the following init script for starting this PHP daemon with php nohup from bash. It works fine (for me at least, please be as nice as you can to me, I have feeling:) but I cant get the PHP daemon to start properly when it's a real boot. When I type sh /etc/init.d/phpdaemons it does start the php daemon with nohup, that's not so nice as thats the freaking point, not needing to do it by hand. So, please, anyone? What I'm I needing to learn here?

Tnx in advanced Daniel

OS specs after code

#!/bin/bash
### BEGIN INIT INFO
#
# Provides: phpdaemons
# Required-Start: 2 3 4 5 6
# Required-Stop: 0 1 6
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PHP nohup daemons initscript
# Description: This file should be placed in /etc / init d
#
### END INIT INFO#

# Fill in name of php daemon file and run as daemon 
PROG="twitter_daemon_spider.php"
PROG_PATH="/home/some/domain/beta.n/lib"
PROG_ARGS=""
PID_PATH="/var/run"

## 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
 else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.
           nohup php  $PROG_PATH/$PROG $PROG_ARGS 2>&1 >/dev/null &
           echo "nohup php $PROG.pid started"
        touch "$PID_PATH/nohup php $PROG.pid"
    fi
}

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

        killall php $PROG

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

                echo "$PROG stopped"

    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi
}

        case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
 ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
                 ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

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

  • Make a symbolic link ln -s /etc/init.d/phpdaemons /etc/rc2.d/S99phpdaemons and then reboot to check..by default Ubuntu boots into runlevel 2..change /etc/rc2.d accrodingly if you are booting into some other runlevel..although as 14.04 uses Upstart, runlevels make very little sense.... – heemayl Jul 3 '15 at 23:22
  • Thans for the help. Will try tomorrow for reliable testing ground (it's 01:33 here) but will let you know! – Daniel Mulder Jul 3 '15 at 23:34
  • Hi @heemay i'm still missing something here. The status is that the init script phpdaemons in /etc/init.d/phpdaemons runs perfectly when the system is running and invoked from the command line, but at reboot it doesn's launch the php script. It all works except for the part that after boot I need to start > stop or reload to launch the php file. I included a link to a doc on my drive as this comment box is to small. I described what I did step by step. If able to look at it for me that would be great! drive.google.com/… – Daniel Mulder Jul 4 '15 at 15:04
  • you are overdoing it..only sudo update-rc.d phpdaemons defaults 98 02 is sufficient..creating link is an alternative.. – heemayl Jul 4 '15 at 22:33
  • thought so, thanks for for you time and advice. – Daniel Mulder Jul 6 '15 at 9:59
0

To make it work we first need to change the current working folder of the bash script that is envoking the daemon.php file as a system serice before using the nohup php command. To make below work: place in (as root) /etc/init.d/yourscript chmod yourscript +x update-rc.d yourscript defaults 98 02 add line to rc2.d: /etc/init.d/yourscript start Alternative add the nohup php yourscript.php directlty to rc.local but this is not the same as running it as a system service like below.

#!/bin/bash
### BEGIN INIT INFO
#
# Provides: phpdaemons
# 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 nohup daemons initscript
# Description: This file should be placed in /etc / init d
#
### END INIT INFO#

# Fill in name of php daemon file and run as System32 Daemon /
PROG="daemon.php"
PROG_PATH="/home/some/domain/beta.nl/daemon/lib"
PROG_ARGS=""
PID_PATH="/var/run"

## 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
 else
        ## Change from /dev/null to something like /var/log/$PROG if you want to save output.

        cd /home/some/domain/beta.nl/daemon/lib/
        nohup php /home/some/domain/beta.nl/daemon/lib/daemon.php > /dev/null &
        echo "nohup php $PROG.pid started"
        touch "$PID_PATH/nohup php $PROG.pid"
    fi
}

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

        killall php $PROG

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

                echo "$PROG stopped"

    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi
}

        case "$1" in
    start)
        start
        exit 0
    ;;
    stop)
        stop
        exit 0
 ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
                 ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac
 stop)
        stop
        exit 0
 ;;
    reload|restart|force-reload)
        stop
        start
        exit 0
                 ;;
    **)
        echo "Usage: $0 {start|stop|reload}" 1>&2
        exit 1
    ;;
esac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.