Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedHat daemon function usage

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.

like image 424
quickshiftin Avatar asked Feb 12 '14 23:02

quickshiftin


People also ask

What is the function of a Linux daemon?

What is a Daemon in Linux? A daemon (usually pronounced as: day-mon , but sometimes pronounced as to rhyme with diamond ) is a program with a unique purpose. They are utility programs that run silently in the background to monitor and take care of certain subsystems to ensure that the operating system runs properly.

What daemon means?

usually daemon : an attendant (see attendant entry 2 sense 1) power or spirit : genius. 3. usually daemon, mythology : a supernatural being whose nature is intermediate between that of a god and that of a human being.

What is service and daemon?

A daemon is a subset of services that always run in memory waiting to service a request. For example - crond , ftpd ,etc. Whereas, a Service is a server application or set of applications that runs in the background waiting to be used, or carrying out essential task.


1 Answers

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.

like image 151
jnas Avatar answered Sep 20 '22 01:09

jnas