Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Using Upstart / init with PHP daemon nohup php daemon.php

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Karlie Kloss

User

( 6 months ago )

 

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

usr_profile.png
Deepak Parmar

User

( 6 months ago )

 

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
}

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif