There are several solutions to this problem. If you use bash, then the shell variable $! will contain the PID of the last forked child process. So, after starting your Java program, do something like:
echo $! > /var/run/my-process.pid
Then after the init script stops the Java process:
pid=$(cat /var/run/my-process.pid)
for count in $(1 2 3 4 5 6 7 8 9 10); do
sleep 1
cat "/proc/$pid/cmdline" 2>/dev/null | grep -q java
test $? -ne 0 && pid="" && break
done
if [ ! -z "$pid" ]; then
echo "Not stopping; terminating with extreme prejudice."
kill -9 $pid
fi
Be sure to remove the pidfile when done.
source
share