Wednesday, 17 February 2016

Play framework Start/Stop Script.

Great post how to exec Play framework as a demon:
http://vladsprogrammingblog.blogspot.co.il/2014/01/play-startstop-script.html?showComment=1455741528114#c2535977690511914718

Play Start/Stop Script.

Here is a start/stop script that I use for my Play! 2.2.x applications.
#!/bin/bash

println() {
    echo
    echo $1
    echo
}

get_or_else() {
        if [ "x" = "x$1" ]
          then echo $2
          else echo $1
        fi
}

check_status() {
    if [ -f $PID_FILE_PATH ]
    then
        kill -0 $( cat $PID_FILE_PATH ) 2> /dev/null
        echo $?
    else
        echo 1
    fi
}

STARTUP_CONFIGURATION_FILE_PATH=/etc/default/play

if [ ! -f "$STARTUP_CONFIGURATION_FILE_PATH" ]
then
  println "Please set STARTUP_CONFIGURATION_FILE_PATH variable in the $0"
  exit 1
fi

source $STARTUP_CONFIGURATION_FILE_PATH

if [ ! -d "$BASE_DIR" ]
then
  println "Please set BASE_DIR variable in the $STARTUP_CONFIGURATION_FILE_PATH"
  exit 1
fi

OPTS="-server"
START_DELAY=3
STOP_DELAY=3

CMD=$BASE_DIR/bin/myapp

PORT=$(get_or_else "$PORT" '9000' )
LISTEN_IP=$(get_or_else "$LISTEN_IP" '0.0.0.0' )
PID_FILE_PATH=$(get_or_else "$PID_FILE_PATH" "$BASE_DIR/play.pid" )
STDOUT_LOG=$(get_or_else "$STDOUT_LOG" '/dev/null' )
HEAP_START_SIZE=$(get_or_else "$HEAP_START_SIZE" '128M' )
HEAP_MAX_SIZE=$(get_or_else "$HEAP_MAX_SIZE" '1024M' )

OPTS="$JAVA_OPTS -Dpidfile.path=$PID_FILE_PATH -J-Xms$HEAP_START_SIZE -J-Xmx$HEAP_MAX_SIZE"

if [ "x" != "x$CONFIGURATION_FILE_PATH" ]
then
  OPTS="$OPTS -Dconfig.file=$CONFIGURATION_FILE_PATH"
fi

if [ "x" != "x$LOG_CONFIGURATION_FILE_PATH" ]
then
  OPTS="$OPTS -Dlogger.file=$LOG_CONFIGURATION_FILE_PATH"
fi

COMMAND="$CMD $OPTS -Dhttp.address=$LISTEN_IP -Dhttp.port=$PORT"

status() {
    if [ 0 -eq $(check_status) ]
    then
        println "Play! is running: [$( cat $PID_FILE_PATH )]."
    else
        println "Play! is not running."
    fi
}

start() {
    if [ 0 -eq $(check_status) ]
    then
        println "Play! is running. [$( cat $PID_FILE_PATH )]."
    else

        rm $STDOUT_LOG >/dev/null 2>&1

        nohup $COMMAND >>$STDOUT_LOG 2>&1 &

        sleep $START_DELAY

        if [ 0 -eq $(check_status) ]
        then
                println "Started."
        else
                rm $PID_FILE_PATH >/dev/null 2>&1
                println "Could not start Play!, please see [$STDOUT_LOG] for more details."
        fi
    fi
}

stop() {
    if [ 0 -eq $(check_status) ]
    then

        if kill $( cat $PID_FILE_PATH )
        then

          sleep $STOP_DELAY

          if [ 0 -lt $(check_status) ]
          then
            println "Stopped."
          else
            println "Could not stop Play! process. Please stop it manually."
          fi

        else
          println "Could not stop process $( cat $PID )."
        fi

    else
        println "Play! is not running."
    fi
}

case "$1" in
    'start')
            start
            ;;
    'stop')
            stop
            ;;
    'restart')
            stop ;
            start
            ;;
    'status')
            status
            ;;
    *)
            echo
            echo "Usage: $0 { start | stop | restart | status }"
            echo
            exit 1
            ;;
esac

exit 0


To run this script just change value of the CMD=$BASE_DIR/bin/myapp variable in the script and create your configuration file/etc/default/play. Here is an example of configuration file I have.


#Base directory of the play project
BASE_DIR=/opt/app/myapp

#Path to the file with process ID
#PID_FILE_PATH=$BASE_DIR/play.pid

#Path to the file, where process output will be redirected
STDOUT_LOG=$BASE_DIR/play.out

#Play! configuration file
CONFIGURATION_FILE_PATH=$BASE_DIR/../myapp.conf

#Logging configuration file
LOG_CONFIGURATION_FILE_PATH=$BASE_DIR/../logger.xml

#Play! port
PORT=9090

#Play! host
#LISTEN_IP='127.0.0.1'

#Additional JVM options
#JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
JAVA_OPTS="-DapplyEvolutions.default=true -DapplyDownEvolutions.default=true"

#Start size of JVM heap
HEAP_START_SIZE=1024M

#Maximum size of JVM heap
HEAP_MAX_SIZE=1024M

Copy start/stop script to /etc/init.d
cd /etc/init.d
chmod +x script-name
update-rc.d script-name defaults (to remove update-rc.d -f script-name remove)
systemctl daemon-reload

No comments:

Post a Comment