Running Play 2.0 applications as a Daemon in Debian

4,939 views
Skip to first unread message

Abhishek Kona

unread,
Mar 9, 2012, 7:36:36 AM3/9/12
to play-fr...@googlegroups.com
Hi,

I have a play application, which I need to push to production on a Debian machine, so I have a few questions around that : 

  • When is the onStop method called in the application lifecycle -> Is it called on a kill $PID of the app? Or is there another way to stop the app.
  • Can I create a fat-jar of the application, so that I can have a sinlge executable instead of a lib folder with all the jars. 
  • What is the standard way to run a play app as a daemon, are there any sample scripts etc.
Play is a really powerful framework and have liked it so far. Need to push my app to production to get the maximum satisfaction.

-Abhishek Kona

sun

unread,
Mar 9, 2012, 12:45:25 PM3/9/12
to play-framework

Ben McCann

unread,
Mar 9, 2012, 1:08:49 PM3/9/12
to play-fr...@googlegroups.com
I've been calling it from a Python script in order to daemonize it.  I'm not totally happy that I need my own script for this though.  It would be nice if the Play start script included a daemon option.  Let me know what you decide to do because I'd be interested in any way to make this better.

-Ben


On Friday, March 9, 2012 9:45:25 AM UTC-8, sun wrote:
https://github.com/playframework/Play20/wiki/Production
https://github.com/playframework/Play20/wiki/ProductionDist

Abhishek Kona

unread,
Mar 19, 2012, 1:37:30 PM3/19/12
to play-fr...@googlegroups.com
Still looking and have not found a satisfactory answer.

Ben McCann

unread,
Mar 19, 2012, 2:40:35 PM3/19/12
to play-fr...@googlegroups.com
I submitted a pull request to allow you to create a distribution which is not zipped.  This is the first stage for me in improving the deployment process.  Hope you might find it helpful too.

-Ben


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/U63tUoj8DZ4J.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Gaëtan Renaudeau

unread,
Mar 19, 2012, 6:03:32 PM3/19/12
to play-fr...@googlegroups.com
To deamonize a play application as deamon from the CLI,

play2 start 9090

should actually already do the trick,

but maybe you are not satisfied because it is kind of blocking but you can background it with Ctrl+D when it's load,

maybe there is a SBT option to bypass this?

about the kill question, I think if the application break anormally a RUNNING_PID file should still remaining, so maybe you could ensure it's not running && remove this file, before running the application in your scripts

2012/3/19 Ben McCann <b...@benmccann.com>



--
Gaëtan Renaudeau, @greweb
blog | gaetanrenaudeau.fr

Ben McCann

unread,
Mar 19, 2012, 7:03:59 PM3/19/12
to play-fr...@googlegroups.com
I'm not sure what you mean by "play2 start 9090" as there is no play2 command.  When you run "play dist" it generates a start script.  I don't believe that running that start script runs the app as a daemon.

Guillaume Bort

unread,
Mar 20, 2012, 4:22:00 AM3/20/12
to play-fr...@googlegroups.com
$ screen
$ ./start

Then type

Ctrl+a then d

Your play app will run in background.

--
Guillaume Bort

Gaëtan Renaudeau

unread,
Mar 20, 2012, 7:41:27 AM3/20/12
to play-fr...@googlegroups.com


2012/3/20 Ben McCann <b...@benmccann.com>

I'm not sure what you mean by "play2 start 9090" as there is no play2 command.  When you run "play dist" it generates a start script.  I don't believe that running that start script runs the app as a daemon.

play2 is a symbolic link to my ~/Play20/play script
 

Justin Holmes

unread,
Mar 20, 2012, 8:11:18 AM3/20/12
to play-fr...@googlegroups.com

echo nohup ./yourapp/start -Dhttp.port=80 > log.log 2>&1 &" > deploy.sh
./deploy.sh

Regards

Marcus Downing

unread,
Mar 20, 2012, 11:02:09 AM3/20/12
to play-fr...@googlegroups.com
Starting the service manually isn't suitable for a production server - what happens if the server has a power cut while you're asleep? In those environments you need an init script to start the service automatically, which on most Linux servers are found in /etc/init.d. Here's one I've used on Gentoo:

/etc/init.d/play.myplayapp

#!/sbin/runscript
INSTANCE="${SVCNAME#*.}"
PATH="/data/$INSTANCE"

depend() {
    need net
    use dns
}

start() {
    ebegin "Starting Play! app: $INSTANCE"
    play start "$PATH"
    eend $?
}

stop() {
    ebegin "Stopping Play! app: $INSTANCE"
    play stop "$PATH"
    eend $?
}

A nice feature of this is that you can copy and rename the script to use it for another service - it picks up the file name and starts the Play app with the same name, presuming that your service is in /data/myplayapp , which is obviously not true for most of you. It also only works on Gentoo due to the use of its runscript; the same wouldn't be true of Ubuntu, for example. You'll need to tune the script to make it suitable to your variant of Linux. Have a look at the other services in /etc/init.d and follow their example, and use the appropriate mechanism to activate it. In Gentoo you'd use rc-update add play.myplayapp .

The equivalent on Windows would be to run Play as a service. The details of that are left as an exercise to the reader.

I do think Play would benefit from shipping a standardised mechanism for running as a service on whatever your operating system. To be a bit more professional than my example above, it should use a file configuration like /etc/play.conf to determine the location of the services to start. An ebuild to install Play would be nice as well.

Drew Kutcharian

unread,
Mar 20, 2012, 12:48:01 PM3/20/12
to play-fr...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/TTpCQ5NQCxUJ.

Ben McCann

unread,
Mar 20, 2012, 7:40:21 PM3/20/12
to play-fr...@googlegroups.com
Java Service Wrapper looks interesting.  Ultimately I think that we would want the Play server to include it by implementing WrapperListener.  It's a GPL license though.  Would that stop Play from being able to make use of it?  Would Apache Commons Daemon or YAJSW would be a better fit due to licensing?  I do see though that ActiveMQ uses the Tanukis Java Service Wrapper while still being distributed under the Apache 2.0 License, so perhaps it is fine.

Ben McCann

unread,
Mar 20, 2012, 8:54:40 PM3/20/12
to play-fr...@googlegroups.com
Btw, here's how I've been running it as a daemon with a couple lines of python code:
   with daemon.DaemonContext(stdout = open("%s/stdout.log" % LOG_DIR, "wb"), stderr = open("%s/stderr.log" % LOG_DIR, "wb")):
      subprocess.Popen(["./start", "-Dlogger.resource=logger-prod.xml"], cwd=BIN_DIR)

This is probably not optimal as I imagine I'll have to restart the service manually if I ever restart the server.  However, it's been working fairly well for me so far.


On Tuesday, March 20, 2012 4:40:21 PM UTC-7, Ben McCann wrote:
Java Service Wrapper looks interesting.  Ultimately I think that we would want the Play server to include it by implementing WrapperListener.  It's a GPL license though.  Would that stop Play from being able to make use of it?  Would Apache Commons Daemon or YAJSW would be a better fit due to licensing?  I do see though that ActiveMQ uses the Tanukis Java Service Wrapper while still being distributed under the Apache 2.0 License, so perhaps it is fine.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Drew Kutcharian

unread,
Mar 21, 2012, 1:26:10 AM3/21/12
to play-fr...@googlegroups.com
Java Service Wrapper does more than just daemonizing  the process, it also monitors the JVM and restarts it if it's hung. They have a commercial version too which more bells and whistles. We've been using JSW in production successfully for years now.

I haven't had a chance to integrate it with Play, but it shouldn't be that difficult, after all the Play entry point is play.core.server.NettyServer.main which is just a regular "public static void main(String[] args)" but in Scala.

-- Drew


To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/a_a1gGdKT6wJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Ike

unread,
Mar 21, 2012, 1:00:48 PM3/21/12
to play-fr...@googlegroups.com
You don't need Play to be installed in production. You can run 'play dist' and it will generate a simple "start" script that you can use to start the server (it will be all in a zip file). When the scripts runs, it creates a file called "RUNNING_PID" with the process PID for the running app that you can use to shutdown the server with something like 'kill `cat RUNNING_PID`'.

Ben L

unread,
Jun 5, 2012, 7:32:35 PM6/5/12
to play-fr...@googlegroups.com
try:

start-stop-daemon --pidfile /srv/www/play/MyPlayApp/RUNNING_PID --chuid ben --exec /srv/www/play/MyPlayApp/target/start --background --start -- -Dhttp.port=9001

Ben McCann

unread,
Jun 5, 2012, 7:33:51 PM6/5/12
to play-fr...@googlegroups.com
I've been using supervisor and am pretty happy with it


On Tue, Jun 5, 2012 at 4:32 PM, Ben L <b...@laughlin.com.au> wrote:
try:

start-stop-daemon --pidfile /srv/www/play/MyPlayApp/RUNNING_PID --chuid ben --exec /srv/www/play/MyPlayApp/target/start --background --start -- -Dhttp.port=9001

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/ar2MQeHLDZAJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Ben L

unread,
Jun 8, 2012, 8:12:10 AM6/8/12
to play-fr...@googlegroups.com
Script I have been using.  It's a variation of the original daemon script provided with Play 1.2.4:


#!/bin/bash
# chkconfig: 345 20 80
# description: Play start/shutdown script (variation of Play 1.2.4 script for Play 2.0.1)
# processname: play
#
# Instalation:
# copy file to /etc/init.d
# chmod +x /etc/init.d/play

# chkconfig --add /etc/init.d/play
# chkconfig play on
#     OR on a Debian system
# sudo update-rc.d play defaults

#
# Usage: (as root)
# service play start
# service play stop
# service play status
#


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # #   M A I N  C U S T O M I S A T I O N   S E C T I O N  # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 


# Path to the application
APPLICATION_PATH=/srv/playapps
APPLICATION_NAME=MyPlayApp

# Additional start params
PARAMS="-Dconfig.resource=prod.conf -Dhttp.port=9000"


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# Path to play install folder
PLAY_HOME=/usr/share/play-2.0.1
PLAY=$PLAY_HOME/play
PORT=9001

# Path to the JVM
JAVA_HOME=/usr/lib/jvm/java-6-sun
export JAVA_HOME

# User running the Play process
USER=ben


# source function library
# -> comented out for ubuntu . /etc/init.d/functions
RETVAL=0

cd ${APPLICATION_PATH}

start() {
start-stop-daemon --pidfile ${APPLICATION_PATH}/${APPLICATION_NAME}/RUNNING_PID --chuid ${USER} --exec ${APPLICATION_PATH}/${APPLICATION_NAME}/target/start --background --start -- "${PARAMS}"
echo -n "Started Play Application: ${APPLICATION_NAME}... "
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
echo " - Success"
else
echo " - Failure"
fi
echo
}
stop() {

kill -9 `cat ${APPLICATION_PATH}/${APPLICATION_NAME}/RUNNING_PID`
rm -rf ${APPLICATION_PATH}/${APPLICATION_NAME}/RUNNING_PID
echo -n "Stopping Play Application: ${APPLICATION_NAME}"

RETVAL=$?

if [ $RETVAL -eq 0 ]; then
echo " - Success"
else
echo " - Failure"
fi
echo
}
status() {
${PLAY} status ${APPLICATION_PATH}
RETVAL=$?
}
clean() {
        rm -f ${APPLICATION_PATH}/RUNNING_PID
        #rm -f application2/service.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

virtualeyes

unread,
Jul 5, 2012, 8:52:11 AM7/5/12
to play-fr...@googlegroups.com
Immensely useful this.

Great for crunch-time-production-launch-oh-no-can't-detach-process-wtf-now with play dist created project ;-)

An init script may be a better long-term solution, but for the quick & dirty right now! fix, a god send

Thanks!

>>>> To post to this group, send email to play-framework@googlegroups.com.


>>>> To unsubscribe from this group, send email to

>>>> play-framework+unsubscribe@googlegroups.com.


>>>> For more options, visit this group at
>>>> http://groups.google.com/group/play-framework?hl=en.
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "play-framework" group.

>>> To post to this group, send email to play-framework@googlegroups.com.


>>> To unsubscribe from this group, send email to

>>> play-framework+unsubscribe@googlegroups.com.


>>> For more options, visit this group at
>>> http://groups.google.com/group/play-framework?hl=en.
>>
>>
>>
>>
>> --
>> Gaëtan Renaudeau, @greweb
>> blog | gaetanrenaudeau.fr
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "play-framework" group.

>> To post to this group, send email to play-framework@googlegroups.com.


>> To unsubscribe from this group, send email to

>> play-framework+unsubscribe@googlegroups.com.


>> For more options, visit this group at
>> http://groups.google.com/group/play-framework?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.

> To post to this group, send email to play-framework@googlegroups.com.


> To unsubscribe from this group, send email to

> play-framework+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Guillaume Bort

Vladimir Vlach

unread,
Jul 16, 2012, 9:54:17 AM7/16/12
to play-fr...@googlegroups.com
I struggle with this startup problem as well. I export Play 2 app as per wiki Documentation using:
$ play clean compile stage
  • "$play clean compile stage" creates simple start script which is useless for production IMHO. It requires Ctrl+D to terminate play app.
  • It does create RUNNING_PID, yet, kill `cat RUNNING_PID` does noting
  • as Bel L previous post, startup script works on Ubuntu but unfortunately on CentOS we're missing start-stop-daemon. Some modification is needed I guess.
There are some pull requests to patch play framework 2 to support background=true parameter but nothing in official release.

If I run myproject/start script as intended and use nohup or daemon, how can I terminate this app? I don't think sending kill -9 is intended behavior.

Vlad

Ben McCann

unread,
Jul 16, 2012, 11:10:35 AM7/16/12
to play-fr...@googlegroups.com
Try play dist instead of play stage


To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/anOoRpRHYf4J.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Meglio

unread,
Jul 25, 2012, 6:25:15 AM7/25/12
to play-fr...@googlegroups.com
Play! guys, please give us recommended scripts for each OS in wiki - this will be so helpful.

Chakib Ouhajjou

unread,
Oct 3, 2012, 1:04:32 PM10/3/12
to play-fr...@googlegroups.com
Thanks for the script Ben !!
but I think in this line

RETVAL=$?
 
Should be before this one :

echo -n "Started Play Application: ${APPLICATION_NAME}... "
because you want to catch the result of the start-stop-daemon not the echo command
Reply all
Reply to author
Forward
0 new messages