Philipp Lengauer
Philipp Lengauer

Reputation: 1959

start-stop-daemon and java - how to do it right?

I'm trying to run a java program as a service. My requirements are:

1) start a java program on machine startup

2) restart if the java program crashes

3) execute it in a special directory as a special user

sidenotes: I CANNOT assume this being the only java process running and it would be dangerous to run the service twice by accident.

So far, I've tried implementing it with start-stop-daemon. However, the application is not automatically restarted when it crashes (i.e., terminates with a non-zero exit code). I guess it has something to do, that I need to use the --background and, thus, start-stop-daemon cannot determine the exit code? Am I correct? How do I resolve this issue properly? (I would prefer a solution with system functionality only, it would be much easier without third party tools due to security restrictions)

My current script (Dummy is, as the same says, a dummy java application which sleeps forever)

#!/bin/sh
### BEGIN INIT INFO
# Provides:          CI Master
# Required-Start:    $all
# Required-Stop:     $all
# Should-Start:      $portmap
# Should-Stop:       $portmap
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: CI Master
# Description:       CI Master
### END INIT INFO

SERVICE_NAME="CI Master"
PIDFILE=/var/run/CI_master.pid
USER=ci
DIRECTORY=./master/
EXECUTABLE=/usr/bin/java
ARGUMENTS="Dummy"

. /lib/lsb/init-functions

case "$1" in
start)
    log_daemon_msg "Starting $SERVICE_NAME" "$SERVICE_NAME"
    start-stop-daemon --pidfile $PIDFILE --make-pidfile --background --chuid $USER --chdir /home/$USER/$DIRECTORY/ --startas $EXECUTABLE --start -- $ARGUMENTS
    log_daemon_msg "$SERVICE_NAME started" "$SERVICE_NAME"
;;
stop)
    log_daemon_msg "Stopping $SERVICE_NAME" "$SERVICE_NAME"
    start-stop-daemon --pidfile $PIDFILE --remove-pidfile --stop
    log_daemon_msg "$SERVICE_NAME stopped" "$SERVICE_NAME"
;;
restart|reload|force-reload)
    $0 stop
    sleep 1
    $0 start
;;
status)
    start-stop-daemon --pidfile $PIDFILE --status
    case $! in
        0)
            log_daemon_msg "$SERVICE_NAME is running" "$SERVICE_NAME"
        ;;
        1)
            log_daemon_msg "$SERVICE_NAME is not running (pid file exists)" "$SERVICE_NAME"
        ;;
        2)
            log_daemon_msg "$SERVICE_NAME is not running" "$SERVICE_NAME"
        ;;
        3)
            log_daemon_msg "unable to determine status of $SERVICE_NAME" "$SERVICE_NAME"
        ;;
    esac
;;
esac
exit 0

Thanks in advance!

Upvotes: 9

Views: 3232

Answers (3)

YangwuWang
YangwuWang

Reputation: 136

I suggest DaemonTools + service/chkconfig.

DaemonTools maybe already installed on your platform, or you can try apt-get. It will restart your daemon automatically in at most 5 seconds.

You can look up linux user manual 8 for more information about service/chkconfig.

Hope this helpful.

Upvotes: 2

kervin
kervin

Reputation: 11858

I use Apache Commons Daemon Tools for this...

JSvc will respawn your Java service process for you...

Jsvc uses 3 processes: a launcher process, a controller process and a controlled process. The controlled process is also the main java thread, if the JVM crashes the controller will restart it in the next minute. Jsvc is a daemon process so it should be started as root and the -user parameter allows to downgrade to an unprivilegded user. When the -wait parameter is used, the launcher process waits until the controller says "I am ready", otherwise it returns after creating the controller process.

JSvc will write a PID File. So the -stop command can also stop your running service.

JSvc -user will allow you to demote your service to a less privileged user after startup.

It is also compatible with ProcRun.exe which works well on Windows.

Upvotes: 1

Alex Karasev
Alex Karasev

Reputation: 1128

I can recommend another solution - Java Service Wrapper http://wrapper.tanukisoftware.com/doc/english/introduction.html

Upvotes: 0

Related Questions