Today, I would like to share one of the script that runs Play Framework Projects as service in linux. We as administrators are always in need to make any of the projects a typical service that can be handled by executing commands like “service play start”, “service play stop” or anything that runs Play Framework in background (silent mode) as service in linux.
What is Play?
Play Website says: “Play is a high-productivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development. Play is based on a lightweight, stateless, web-friendly architecture and features predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications thanks to its reactive model, based on Iteratee IO.”
Why Play?
In Sphata Systems, we are developing a platform “HUB” that is purely a cloud friendly platform. Before even we stared thinking about the design, first we started understanding the problem of any application that is / to be hosted in cloud if its scalable, flexible, modularized and so on. We clearly understood the demand of any cloud application and we started dismantling each and every block of the system in such a way that each block / module shall be treated as standalone, scalable services that runs and grows on its own. This design enables us to upscale or downsize the computing capacity of the instance that hosts any service in cloud. We in Sphata are trying to come up with a patent-able approach to design a cloud based platform.
For highly demanding design like this, we were in need of a front-end web container that has the capacity for distribution in cloud and flexible enough for us to develop and manage within our custom containers. For this, we chose “Play Framework”. We use Play to manage all VIEWS / GUI that interacts directly to our service containers via web calls.
Need for Startup Scripts:
Play Framework supports CI greatly and even though there are quite a lot of options in play to package and build for production deployment, there is no such great service wrappers in built. But, as administrators, we will be in need of a startup script that starts the project and run silently. Also, we should be in a position to stop, start and manage like any other service in linux. Even though there are lot of scripts available in forums to automate Play Project to run as service in Linux, I took one good example from Play’s google groups and slightly customized to fit my needs. This works fine for me in my cloud instance.
Startup Script to run Play as Service
Just copy this code and make a file as “play” and save it to “/etc/init.d” and then do “chmod +x play”. Now the script is ready to run as executable service for you.
#!/bin/bash
# chkconfig: 345 20 80
# description: Play start/shutdown script
# processname: play
#
# Installation:
# copy file to /etc/init.d
# chmod +x /etc/init.d/play
# chkconfig --add /etc/init.d/play
# chkconfig play on
#
# Usage: (as root)
# service play start
# service play stop
# service play status
#
# Remember, you need python 2.6 to run the play command, it doesn't come standard with RedHat/Centos 5.5
# Also, you may want to temporarily remove the >/dev/null for debugging purposes
# Path to play install folder
PLAY_HOME=/var/play
PLAY=$PLAY_HOME/play
# User running the Play process
USER=root
# Path to the application
APPLICATION_PATH=/var/play/helloworld
# source function library
. /etc/init.d/functions
RETVAL=0
cd ${APPLICATION_PATH}
start() {
echo -n "Starting Play service: "
su -s /bin/sh $USER -c "target/start >/dev/null &"
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo_success
else
echo_failure
fi
echo
}
stop() {
su -s /bin/sh $USER -c "kill -9 `cat ${APPLICATION_PATH}/RUNNING_PID` >/dev/null"
su -s /bin/sh $USER -c "rm -rf ${APPLICATION_PATH}/RUNNING_PID >/dev/null"
# su -s /bin/sh $USER -c "play clean stage >/dev/null"
echo -n "Stopping Play Application: ${APPLICATION_PATH}"
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo_success
else
echo_failure
fi
echo
}
status() {
su -s /bin/sh $USER -c "${PLAY} state"
RETVAL=$?
}
clean() {
su -s /bin/sh $USER -c "rm -f ${APPLICATION_PATH}/server.pid"
su -s /bin/sh $USER -c "rm -f ${APPLICATION_PATH}/RUNNING_PID"
}
case "$1" in
start)
clean
start
;;
stop)
stop
;;
restart|reload)
stop
sleep 10
start
;;
status)
status
;;
clean)
clean
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac
exit 0
After copying the above script to /etc/init.d/ you shall execute as
# chmod +x /etc/init.d/play
# service play start
....
# service play stop
Note: You need to customize this script based on where you installed Play and where your project is. Otherwise, this script should be able to handle the play as service.
How to use this script for multiple play projects?
This is quite simple as all you have to do is to just clone this service file and then rename to corresponding play_<project_name> and then edit the file with respective path to that project. Then start it as a different service.