14

I'm working on an init script for Jetty on RHEL. Trying to use the daemon function provided by the init library (/etc/rc.d/init.d/functions).

I found this terse documentation, and an online example (I've also been looking at other init scripts on the system for examples).

Look at this snippet from online to start the daemon

daemon --user="$DAEMON_USER" --pidfile="$PIDFILE" "$DAEMON $DAEMON_ARGS &"
RETVAL=$?
pid=`ps -A | grep $NAME | cut -d" " -f2`
pid=`echo $pid | cut -d" " -f2`
if [ -n "$pid" ]; then
        echo $pid > "$PIDFILE"
fi

Why bother looking up the $PID and writing it to the $PIDFILE by hand? I guess I'm wondering what the point of the --pidfile option to the daemon function is.

quickshiftin
  • 66,362
  • 10
  • 68
  • 89

1 Answers1

11

To answer the question you guess that you have, is that --pidfile is used to check whether the daemon process is already running. On RHEL (and derivates) the daemon function won't write the pidfile.

In the case that the program stays in the foreground it has to be explicitly sent to the background by appending & to the command and the pid has to be fetched afterwards. $! is not usable when using daemon.

jalopaba
  • 8,039
  • 2
  • 44
  • 57
jnas
  • 826
  • 10
  • 14
  • 2
    The deamon command does not write the pid file, AND does not deamonize the program, seems.. strange. – JAR.JAR.beans Dec 10 '17 at 16:14
  • `daemon` is a function from `initscripts` which `Starts a daemon, if it is not already running. Does other useful things like keeping the daemon from dumping core if it terminates unexpectedly.` – jnas Dec 12 '17 at 07:43
  • 1
    `starts a deamon` is no precise as daemon by specification is a background process. if I need to add a nohup or & on the method, this seems very counter intuitive to me. – JAR.JAR.beans Dec 12 '17 at 15:05