Skip to content

Instantly share code, notes, and snippets.

@skakri
Created December 17, 2012 19:33
Show Gist options
  • Save skakri/4321244 to your computer and use it in GitHub Desktop.
Save skakri/4321244 to your computer and use it in GitHub Desktop.
Dreamwidth worker-manager init.d script
#!/bin/bash
# /etc/init.d/worker-manager
# version 0.1 2012-12-17 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: worker-manager
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: DW worker manager
# Description: Starts DW worker manager
### END INIT INFO
#Settings
SERVICE='worker-manager'
OPTIONS='' # e.g --debug, also could be used to route output to a logfile
USERNAME='dw'
LJHOME='/home/dw'
INVOCATION="$LJHOME/bin/worker-manager $OPTIONS"
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
wm_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $LJHOME
as_user "cd $LJHOME && $INVOCATION"
sleep 1
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE started."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
wm_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "pkill $SERVICE"
sleep 3
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
#Start-Stop here
case "$1" in
start)
wm_start
;;
stop)
wm_stop
;;
restart)
wm_stop
wm_start
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment