Skip to content

Instantly share code, notes, and snippets.

@h4rm0n1c
Last active August 29, 2015 14:19
Show Gist options
  • Save h4rm0n1c/437336ed9f7d805f9d83 to your computer and use it in GitHub Desktop.
Save h4rm0n1c/437336ed9f7d805f9d83 to your computer and use it in GitHub Desktop.
Planetary Annihilation NodePAMaster init.d service control script
#!/bin/bash
# /etc/init.d/planetary
# version 0.0.2 2015-04-25 (YYYY-MM-DD)
# caveat emptor, don't copy and paste and just hope it'll work, READ AND COMPREHEND.
### BEGIN INIT INFO
# Provides: planetary
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: planetary annihilation server
# Description: Starts the planetary annihilation server
### END INIT INFO
#Settings
#set this a word out of the server's name as specified in your nodepamaster config, do not put in spaces, just one word or pgrep will be unhappy.
#this just helps the script to find the server process itself via its name, for status monitoring.
SERVICE='GOAT'
#this is the user the server is running under
USERNAME='planetary'
#base home dir for pa user, papatcher should be here, not using this yet, but prep for update command being written.
PAPATH='/home/planetary'
#nodepamaster folder should be in the pa user's home dir
NODEPAMASTER="${PAPATH}"/NodePAMaster/src
#nodepamaster invocation command
INVOCATION="nodejs control.js"
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
pa_start() {
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd "${NODEPAMASTER}"
as_user "cd '${NODEPAMASTER}' && screen -dmS planetary $INVOCATION"
sleep 2
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
pa_stop() {
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S planetary -X quit"
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
pa_update() {
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "$SERVICE is running! Will not start update."
else
echo "TODO: Update Script"
fi
}
#Start-Stop here
case "$1" in
start)
pa_start
;;
stop)
pa_stop
;;
restart)
pa_stop
pa_start
;;
update)
pa_stop
pa_update
pa_start
;;
status)
if pgrep -u $USERNAME -f "${SERVICE}" > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: /etc/init.d/planetary {start|stop|update|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