views:

632

answers:

1

I have a process that is running as a java daemon on Ubuntu Linux.

I cannot stop it using start-stop-daemon command in d_stop() below.

The process id should be written to file $PIDFILE during the start process, but that is not working.

Here is my script:

#! /bin/sh
#
#
#
# Version:      @(#)daemon  1.0
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="xxxxx"
NAME="xxxxx"
USER="root"
HOME="/home/root"
MAIN="/opt/MYAPP/lib/NodeManager.jar"
APP_JAVAHOME="/home/owner/jdk1.6.0_17"
DAEMON="$APP_JAVAHOME/bin/java -server -Djava.awt.headless=true -jar $MAIN"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.

test -x $APP_JAVAHOME/bin/java || exit 0

# ---------------------------------------
# Function that starts the daemon/service
# ---------------------------------------
d_start()
{
su -p -s /bin/sh - $USER -c "$DAEMON &> /dev/null & echo $!" > $PIDFILE
}


# --------------------------------------
# Function that stops the daemon/service
# --------------------------------------
d_stop()
{
start-stop-daemon --stop --quiet --pidfile $PIDFILE
#/bin/ps -ef | grep java | grep -v grep | awk '{print $2}
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
+1  A: 

Is there any reason you can't use start-stop-daemon to start the process - something like this should be what you're looking for:

DAEMON="$APP_JAVAHOME/bin/java"
ARGS="-server -Djava.awt.headless=true -jar $MAIN"

start-stop-daemon --start --pidfile "$PIDFILE" --chuid "$USER" --background --make-pidfile --startas "$DAEMON" -- $ARGS

If you need any environment variables set, set them in the startup script and export them.

caf
This works but the PID file still doesn't have the PID in it? Will stopping this work ok?
Here's a clue. The PID file has some output from the process that was running. Am I suppose to redirect that somewhere else?
Adding -m did the trick!
I can't upvote :(
Ah yes, I've edited my answer to add `--make-pidfile` to the options.
caf