Domanda

I've been trying to write a startup script for my play2 application but I can't get it to work as a background task. I started out at the docs and came up with the script below.

Script:

#! /bin/sh

# description: Starts autocomplete play app using daemon
# 1. Go to $APPLICATION_PATH and prepare for dev by doing: play clean compile stage
#    This will create a start script at target/start
# 2. Start the application by running this script

. /etc/rc.d/init.d/functions

PLAY_HOME=/opt/play
PLAY=$PLAY_HOME/play
NAME=autocomplete
DESC="autocomplete application"
PID_FILE=/var/run/autocomplete/$NAME.pid    

# Path to the JVM
JAVA_HOME=/usr/java/latest
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

APPLICATION_PATH=/opt/playapps/autocomplete

DAEMON_OPTS="-Dconfig.file=/opt/playapps/autocomplete/conf/application-dev.conf"

start()
{
    echo -n "Starting $DESC with: --pidfile $PID_FILE ${APPLICATION_PATH}/target/start $DAEMON_OPTS"
    daemon --pidfile $PID_FILE "${APPLICATION_PATH}/target/start $DAEMON_OPTS"
}
stop()
{
  echo -n $"Stopping $DESC:"
  #NOT DONE YET
}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
esac

exit $RETVAL

I have google the issue and found solutions like this but they are using the start-stop-daemon that I do not have on my Red Hat Enterprise Linux Server release 5.6 (Tikanga) dist. I would rather not install any other software to make this work if it's possible. What have I missed to make this work as a background task and detach it from the console? CTRL+D do not work and CTRL+C quits the process.

È stato utile?

Soluzione

Ok here is one solution after tips from favoretti:

#! /bin/sh

# description: Starts autocomplete play app using daemon
# 1. Go to $APPLICATION_PATH and prepare for dev by doing: play clean compile stage
#    This will create a start script at target/start
# 2. Start the application by running this script
# created by: Jakob

. /etc/rc.d/init.d/functions

PLAY_HOME=/opt/play
PLAY=$PLAY_HOME/play
NAME=autocomplete
DESC="autocomplete application"

# Path to the JVM
JAVA_HOME=/usr/java/latest
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

APPLICATION_PATH=/opt/playapps/autocomplete

DAEMON_OPTS="-Dconfig.file=/opt/playapps/autocomplete/conf/application-dev.conf"

start()
{
        echo -n "Starting $DESC with: $APPLICATION_PATH/target/start $DAEMON_OPTS &"
        $APPLICATION_PATH/target/start $DAEMON_OPTS &
}
stop()
{
        echo -n $"Stopping $DESC:"
        kill `cat $APPLICATION_PATH/RUNNING_PID`
}


case "$1" in
  start)
    start
    ;;
  stop)
        stop
        ;;
esac

exit $RETVAL

You will need to CTRL+D to get out of the console which isn't exactly what I had in mind. Maybe there is someone out there with a better solution?

Altri suggerimenti

Here is my version to auto start a play 2.2 application under Amazon AMI Linux. The same should work under CentOS and RedHat. Other linux flavors could need little adaption. The script starts the play app under separate user.

#!/bin/bash
# description: MyWorkCalendar Play App
# processname: myworkcalendar
# chkconfig: 234 20 80

# User running the Play process
USER=mywork
USER_HOME=/home/mywork

# Java home, add java and play to path
export JAVA_HOME=$USER_HOME/java_home
export PATH=$JAVA_HOME/bin:$USER_HOME/play_home:$PATH

# Path to the application
APP_PATH=$USER_HOME/app/work-calendar/target/universal/stage
APP_OPTS="-Dconfig.file=$APP_PATH/conf/application-prod.conf"

RETVAL=0

case "$1" in
  start)
    echo -n "Starting Play service"
    rm -f ${APP_PATH}/RUNNING_PID
    su $USER -c "$APP_PATH/bin/myworkcalendar $APP_OPTS >/dev/null" &
    RETVAL=$?
    ;;
  stop)
    echo -n "Shutting down Play service"
    kill `cat $APP_PATH/RUNNING_PID`
    RETVAL=$?
    ;;
esac
exit $RETVAL

Put the script to /etc/init.d, make it executable and register it with chkconfig:

sudo chmod 755 script-name
sudo chkconfig --add script-name
sudo chkconfig script-name on

Here is my version of the start/stop script for Play 2.2:

http://vladsprogrammingblog.blogspot.com/2014/01/play-startstop-script.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top