-
-
Save Itaqua/3866022 to your computer and use it in GitHub Desktop.
init.d script to launch Play framework under Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Nov. 2012 | |
# Octavio Luna & Arturo Lopez | |
### BEGIN INIT INFO | |
## END INIT INFO | |
# Launch play 1.x as a service | |
# Install | |
# sudo update-rc.d play_ubuntu_init.sh defaults 80 | |
# | |
# see man update-rc.d for advanced features. | |
# Usage: (as root) | |
# service play start [service] | |
# service play stop [service] | |
# service play status [service] | |
# service play clean [service] | |
# you may want to temporarily remove the >/dev/null for debugging purposes | |
# Path to play install folder | |
PLAY_HOME=/opt/playframework/play-1.2.5 | |
PLAY=$PLAY_HOME/play | |
# Path to the JVM | |
# openJDK | |
#JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk | |
# oracleJDK | |
JAVA_HOME=/usr/lib/jvm/java-7-oracle | |
export JAVA_HOME | |
# User running the Play process | |
USER=www-data | |
# Path to the application | |
#APPLICATION_PATH=/var/www/playapps/NAME_OF_APPLICATION | |
APPLICATIONS_BASE="/opt/webapps/" | |
APPLICATIONS=("test1" "test2") | |
APPLICATION_MODE=prod | |
. /lib/lsb/init-functions | |
# HELPER FUNCTIONS | |
function startService { | |
app=$1 | |
APPLICATION_PATH="${APPLICATIONS_BASE}/${app}" | |
echo -n "Starting Play service: \"${app}\"" | |
if [ -f ${APPLICATION_PATH}/server.pid ]; then | |
echo " ...running already" | |
else | |
su $USER -c "${PLAY} start ${APPLICATION_PATH} --%${APPLICATION_MODE} >/dev/null" | |
RETVAL=$? | |
# You may want to start more applications as follows | |
# [ $RETVAL -eq 0 ] && su $USER -c "${PLAY} start application2" | |
# RETVAL=$? | |
if [ $RETVAL -eq 0 ]; then | |
log_end_msg 0 | |
else | |
log_end_msg 1 | |
fi | |
fi | |
} | |
function stopService { | |
app=$1 | |
APPLICATION_PATH="${APPLICATIONS_BASE}/${app}" | |
echo -n "Shutting down Play service: \"${app}\"" | |
${PLAY} stop ${APPLICATION_PATH} > /dev/null | |
RETVAL=$? | |
if [ $RETVAL -eq 0 ]; then | |
log_end_msg 0 | |
else | |
log_end_msg 1 | |
fi | |
} | |
function statusService { | |
app=$1 | |
APPLICATION_PATH="${APPLICATIONS_BASE}/${app}" | |
echo -n "Status of Play service: \"${app}\" - " | |
${PLAY} status ${APPLICATION_PATH}|grep Started | |
RETVAL=$? | |
if [ $RETVAL -ne 0 ]; then | |
log_end_msg 1 | |
fi | |
} | |
function statusFullService { | |
app=$1 | |
APPLICATION_PATH="${APPLICATIONS_BASE}/${app}" | |
${PLAY} status ${APPLICATION_PATH} | |
RETVAL=$? | |
} | |
function cleanService { | |
app=$1 | |
APPLICATION_PATH="${APPLICATIONS_BASE}/${app}" | |
echo -n "Cleaning Play service: \"${app}\" - " | |
pid=`cat ${APPLICATION_PATH}/server.pid` | |
echo -n "(${pid})" | |
kill -9 $pid | |
rm -f ${APPLICATION_PATH}/server.pid | |
log_end_msg 0 | |
} | |
case "$1" in | |
start) #Start Service | |
if [ $2 ]; then | |
startService $2 | |
else | |
for app in "${APPLICATIONS[@]}" | |
do | |
startService $app | |
done | |
fi | |
;; | |
stop) #Stop Service Gracefully | |
if [ $2 ]; then | |
stopService $2 | |
else | |
for app in "${APPLICATIONS[@]}" | |
do | |
stopService $app | |
done | |
fi | |
;; | |
status) #Status of the service | |
if [ $2 ]; then | |
statusFullService $2 | |
else | |
for app in "${APPLICATIONS[@]}" | |
do | |
statusService $app | |
done | |
fi | |
;; | |
clean) # Erase server.pid of selected applications USE AS LAST RESOURCE AFTER CHECKING MEMORY FOR RUNNING PROCESSES | |
if [ $2 ]; then | |
cleanService $2 | |
else | |
for app in "${APPLICATIONS[@]}" | |
do | |
cleanService $app | |
done | |
fi | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|status|clean} [service]" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment