Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a problem with killing my Java Program on Ubuntu. I created the following init script:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          control ...
# Required-Start:    $remote_fs $network $syslog
# Required-Stop:     $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ...
# Description:       ...
### END INIT INFO

DAEMON_PATH="/mnt/Data/wstlm"
DAEMON=startWstlm
DAEMONOPTS=""

NAME=wstlm
DESC="My daemon description"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
        printf "%-50s" "Starting $NAME..."
        cd $DAEMON_PATH
        PID=`./$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
        echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -9 $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;

restart)
    $0 stop
    $0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac

This init script changes the directory to my program and the content of the executed startWstlm is:

#!/bin/sh
echo date +"%Y/%m/%d %T" - start WSTLm 1>>/mnt/Data/wstlm/wstlm.log
java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar

I added the init script to the init.d. If i call

sudo service myProgram start

The program starts properly. But if I use

sudo service myProgram stop

The Program doesn't shut down. Only the bash command which started my java program is killed.

Before executing stop:

1 root /bin/sh ./startWstlm
2 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar
3 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar
4 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar

After executing stop:

(note this bash was killed)
2 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar
3 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar
4 root java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar

(By the way: why there are multiple processes of this jar?)

So the problem is that I cant stop my program. What is kind of .. annoying. Has anyone an idea?

share|improve this question

migrated from stackoverflow.com Mar 19 '13 at 8:50

This question came from our site for professional and enthusiast programmers.

    
I'm surprised this was migrated; it seems like a fairly standard problem in shell programming. –  chepner Mar 19 '13 at 22:46
add comment

1 Answer

First, don't use kill -9; a simple kill should be sufficient. Second, see this page for a good explanation of how to kill child processes when the parent is told to exit. In this case, startWstim needs to take care of killing the Java program before it exits.

Finally, it might be simpler to dispense with startWstim, and instead execute the commands it contains directly in your init script:

start)
    printf "%-50s" "Starting $NAME..."
    cd $DAEMON_PATH
    echo "$(date +"%Y/%m/%d %T") - start WSTLm" >> /mnt/Data/wstlm/wstlm.log
    java -Djavax.net.ssl.trustStore=jssecacerts -jar /mnt/Data/wstlm/myProgram.jar &
    PID=$!

Note that you don't need a subshell to get the process ID of the Java program, and you need to capture the output of the date command in order to print it to the log file.

share|improve this answer
    
Thank you for your efforts. I followed your approach to use kill and implement the start commands in the init script only. –  No3x Mar 25 '13 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.