javalinuxbashdaemonstart-stop-daemon

how to create a PID file for a java process


I'm attempting to create a PID file like below:

LOCKFILE=/var/lock/ocr-trafficcop.lock
PIDFILE=/var/run/ocr-trafficcop.pid
JVM_ARGS="-Xms1024M -Xmx2048m -Dspring.config.location=/opt/app/configs/ocr-trafficcop/ -DserviceName=ocr-trafficcop -DAPP_ENV=int -Dlog.home=/opt/app/logs -Dservice.home=/opt/app"
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
PROGRAM=/usr/bin/java
PROGRAM_ARGS="$JVM_ARGS -jar $JAR_PATH"
#
. /etc/init.d/functions
#
daemon --user=$USER "$PROGRAM $PROGRAM_ARGS > /dev/null 2>&1 & echo $! > $PIDFILE"

the PID file is empty though, so how do I create/populate a PID file ?


Solution

  • Basically, it seems daemon doesn't really write pid files, from a brief search online. However,something like this should work:

    su -c "$PROGRAM $PROGRAM_ARGS >/dev/null 2>&1 &"
    PID=$!
    echo $PID > $PIDFILE
    

    From here:

    https://kiennt.com/blog/2012/06/29/run-program-and-generate-pid.html