1

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 ?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Simply Seth
  • 3,246
  • 17
  • 51
  • 77
  • Java can [return its own pid](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ProcessHandle.html#current()), so the Java program can write its own PID file. – VGR Oct 01 '18 at 22:03
  • 1
    I think you want `|| echo $! $PIDFILE` there at the end (use `||` rather than `&` ) – moilejter Oct 01 '18 at 22:11

1 Answers1

3

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

mjuarez
  • 16,372
  • 11
  • 56
  • 73