Creating a startup script for your play program

Posted on Tuesday, June 18, 2013


I am running a play server on Ubuntu 12.10.  I wanted to create a simple start/stop script in the /etc/init.d folder for it.

Here are the steps I took to accomplish this.





Difference between run, start, and stage


I am still new to the Scala Play Framework so bare with me.  I still do not have it fully in my mind yet, but I will tell you what I do understand.  I gleamed a lot of this information from Plays own documentation at http://www.playframework.com/documentation/2.1.1/Production  [1]

Run


Using run to start your play program is meant for development. 
Here are some examples of using the run command.


> play ~run


Or


> play run



Run is only meant for dev as a complete check is handled by sbt.

As a side note…
The "~" before a command will trigger the command when files are updated.    So ~run will reload every time a source file is changed.





Start


From the Play documentation

 Play forks a new JVM and runs the default Netty HTTP server



> play start







Stage


From the documentation

You can use the stage task to prepare your application to be run in place


You can compile the program, which creates .jar files and removes the need for the compiled code to rely on Scala in any way.  This also speeds up the initialization of your play server.


To do this run


> play clean compile stage



This creates packages the jar files you need and creates a start script in the newly created target folder.

To run the script issue the following command


> target/start



My current plan is to compile the server with its start script then deploy it to my server and use a script in the /etc/init.d folder to start/stop the play server.

If you look in target/staged you should see the compiled jar files the start script relies on


> ls -alh target/staged/









Create the /etc/init.d


I am creating a start/stop script for my play server at /etc/init.d/play-server.

Open the file for editing


> sudo vi /etc/init.d/play-server


And place the following into it




#!/bin/bash

APPLICATION_PATH=/www/play

start() {
    echo -n "Starting"
    sudo start-stop-daemon --start --background --pidfile ${APPLICATION_PATH}/RUNNING_PID -d ${APPLICATION_PATH} --exec target/start -- -Dhttp.port=4200
    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}
stop() {
    echo -n "Stopping"
    sudo start-stop-daemon --stop --pidfile ${APPLICATION_PATH}/RUNNING_PID

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}

  case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: play-server {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL



Update its permissions


> sudo chmod 755 /etc/init.d/play-server



Then from the command line you can run


> sudo /etc/init.d/play-server start




To stop it run


> sudo /etc/init.d/play-server stop





To make it start on a server reboot run the following command


> sudo update-rc.d play-server defaults



Reboot the server to test it


> sudo reboot now







References
[1]        Starting your application in production mode      
                Accessed 06/2013


3 comments:

  1. That's an interesting strategy. how do you feel about doing it the catalina.sh way?

    ReplyDelete
  2. Nice! I just used this to deploy our first official Play app to QA. Thanks.

    ReplyDelete
    Replies
    1. I am glad it was of some use to someone. If you see any ways to improve upon it please share :)

      Delete