Skip to content

Instantly share code, notes, and snippets.

@bchazalet
Last active August 29, 2015 14:04
Show Gist options
  • Save bchazalet/2c0b16e7c5278fd2f3c8 to your computer and use it in GitHub Desktop.
Save bchazalet/2c0b16e7c5278fd2f3c8 to your computer and use it in GitHub Desktop.
A basic init script (start/status/stop/restart) for a play 2.3.x applications running on a CentOS machine.
#!/bin/bash
# chkconfig: 2345 85 15
# description: starts your play application as a service
############################################################################################
# CentOs init.d start/stop script for your play application packaged with the dist command #
# Tested on CentOS 6.5 #
# You should: #
# 1. Update your project's parameters #
# 2. Copy this file to /etc/init.d/ #
# 3. Make it executable #
# 4. Run chkconfig #
############################################################################################
## Custom project parameters, update them with yours
PLAY_PROJECT_NAME="my-app" # use to get play's start script name
PLAY_ROOT_DIR=/home/boris/my-app # the folder of your play dist files
DAEMON_OPTS="" # options you want to pass to your play command
# starts the daemon as:
USER=boris
## derived parameters
# by default, we will use the project name for the script name (that was play's dist command does)
START_SCRIPT_NAME=$PLAY_PROJECT_NAME
PIDFILE=$PLAY_ROOT_DIR/RUNNING_PID
# this is the command to be run, give the full pathname
DAEMON=$PLAY_ROOT_DIR/bin/$START_SCRIPT_NAME
# contains the runuser() function we will use to start the service as a different user
. /etc/rc.d/init.d/functions
case "$1" in
start)
if [ -f $PIDFILE ]; then
printf "%s\n" "Service already running"
else
printf "%-50s" "Starting $NAME..."
runuser $USER -c "$DAEMON $DAEMON_OPTS > /dev/null 2>&1 &"
echo "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
if [ -f $PIDFILE ]; then
kill $PID
printf "%s\n" "Ok"
else
printf "%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment