Skip to content

Instantly share code, notes, and snippets.

@alexey-detr
Last active December 23, 2015 12:09
Show Gist options
  • Save alexey-detr/6633085 to your computer and use it in GitHub Desktop.
Save alexey-detr/6633085 to your computer and use it in GitHub Desktop.
Wrapper for PHP scripts to deamonize em. It is allowed to specify user for further passing it to PHP script e.g. app/beanstalk-php-worker.sh start www-data
#!/bin/sh
### BEGIN INIT INFO
# Required-Start: beanstalk-job-server
# Default-Start: 2 3 4 5
# Default-Stop:
# Description: Php workers wrapper
### END INIT INFO
WORKDIR="$( cd "$( dirname "$0" )" && pwd )"
PROCESSNAME=beanstalk-php-worker
PIDFILE=/var/run/$PROCESSNAME.pid
WORKER="$WORKDIR/console beanstalk:worker:start"
USER="$2"
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
do_start()
{
if [ -n "$USER" ]
then
USERID=`id -u $USER`
else
USERID=`id -u`
fi
echo "Will run worker with UID $USERID.";
echo "Starting...";
/sbin/start-stop-daemon --start --pidfile $PIDFILE --exec $WORKER -- --uid=$USERID --gid=$USERID --pidfile=$PIDFILE || return 1
echo "OK";
}
do_stop()
{
echo "Stopping...";
/sbin/start-stop-daemon --stop --pidfile $PIDFILE || return 1;
echo "OK";
rm -f $PIDFILE;
}
do_status()
{
if [ -f "$PIDFILE" ];
then
PID=`cat "$PIDFILE"`
echo "PIDFILE found. PID is $PID"
if [ `ps "$PID" | wc -l` -eq 2 ];
then
echo "Service OK."
else
echo "Process #$PID not found in system. Service is down, removing PIDFILE."
`rm -f "$PIDFILE"` > /dev/null
fi
else
echo "PID file not found, seems service is down."
fi
}
case "$1" in
start)
do_start;
;;
stop)
do_stop;
;;
status)
do_status;
;;
restart)
do_stop;
do_start;
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment