Cannot stop Java service on Linux using start-stop-daemon

I have a process that works like a java daemon on Ubuntu Linux.

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

The process ID must be written to the $ PIDFILE file at startup, but this does not work.

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 "."
+3
source share
1 answer

Is there a reason you cannot use start-stop-daemonto start the process - something like this should be what you are 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 them in a startup script and export them.

+2
source

Source: https://habr.com/ru/post/1723955/


All Articles