1

I want deploy a play framework web application from source, and run play start to start the application.

I wrote a startup script in /etc/init.d/ which executes daemon play start when the service starts, but the service start command did not return.

I guess it's because play start is waiting for me to enter Ctrl+D. nohup can fix it, but with nohup, I have to run kill -9 xxx to stop the application, which is not what I like.

What's the best way to run a play framework application from source as a linux startup service?

0

1 Answer 1

0

Here is simple init.d script, which:

  • start: recompiles (if needed) and starts application in background, only if application wasn't already started
  • stop: kills app

Read comments in code carefully.

#!/bin/sh
# /etc/init.d/playapp

# Play project directory is in /var/play/playapp/www, not directly in SDIR
SDIR="/var/play/playapp"

# The following part always gets executed.
echo "PLAYAPP Service"

# The following part carries out specific functions depending on arguments.
case "$1" in
  start)
    echo " * Starting PLAYAPP Service"
    
    if [ -f ${SDIR}/www/target/universal/stage/RUNNING_PID ]
    then        
        PID=$(cat ${SDIR}www/target/universal/stage/RUNNING_PID)
        
        if ps -p $PID > /dev/null
        then
            echo "   service already running ($PID)"
            exit 1
        fi
    fi
    
    cd ${SDIR}/www
    
    # REPLACE "PROJECT_NAME" with your project name
    
    if [ ! -f ${SDIR}/www/target/universal/stage/bin/PROJECT_NAME ]
    then    
        echo "   recompiling..."
        
        # REPLACE path to your play command
        /var/play-install/play/play clean compile stage
    fi

    echo "   starting..."
    nohup ./target/universal/stage/bin/PROJECT_NAME -Dhttp.port=9900 -Dconfig.file=/var/play/playapp/www/conf/application-prod.conf > application.log 2>&1&
    ;;
  stop)
    echo " * Stopping PLAYAPP Service"
    
    if [ ! -f ${SDIR}/www/target/universal/stage/RUNNING_PID ]
    then
        echo "   nothing to stop"
        exit 1;
    fi
    
    kill -TERM $(cat ${SDIR}/www/target/universal/stage/RUNNING_PID)    
    ;;
  *)
    echo "Usage: /etc/init.d/playapp {start|stop}"
    exit 1
    ;;
esac

exit 0

You must log in to answer this question.