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

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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

profilepic.png
manpreet 2 years 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
}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.