Cannot stop java service in Linux using start-stop-daemon
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()
开发者_StackOverflow社区{
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 "."
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.
精彩评论