Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

ubuntu

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!

like image 517
loonytune Avatar asked May 14 '15 17:05

loonytune


People also ask

How start stop daemon works?

start-stop-daemon will scan the process table looking for any processes which match the process name, parent pid, uid, and/or gid (if specified). Any matching process will prevent --start from starting the daemon.

How do you start a daemon?

Starting daemons. Daemons can be started by JCL and also by the shell. Some daemons such as inetd can also be started by the shell. Interactive login shells, shell scripts run as background jobs from a login shell, and batch jobs using BPXBATCH to run the shell all can start daemons.

How do you stop a daemon?

Issue the kill -15 command with the process identifier number to stop the daemons. For AIX® and Linux x86_64 GPFS™ file systems, issue the command dmkilld to stop the recall daemons. Verify that the daemons are no longer running.


1 Answers

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.

like image 183
YangwuWang Avatar answered Sep 28 '22 22:09

YangwuWang