Centos – what is the canonical way to start a daemon in rhel/centos-6 init script

centosinitinit-scriptrhel

I found lots of good documentation for ubuntu's start-stop-daemon and there is a man page for a binary daemon.

But from what I can tell the canonical way to start a daemon in a rhel/centos script is to source /etc/init.d/functions then use the daemon() function. But I can't find any good examples or documentation.

What is the canonical way to start a daemon in rhel/centos-6 init script?

my first attempt was:

#!/bin/bash
source /etc/init.d/functions
daemon --user USER nohup /path/to/your/binary arg1 arg2 >/dev/null 2>&1 &

Best Answer

The documentation and example you are looking for is located at /usr/share/doc/initscripts-*/sysvinitfiles on CentOS/RHEL. Here is the documentation for the daemon function specifically:

daemon [ --check ] [ --user ] [+/-nicelevel] program [arguments] [&]

    Starts a daemon, if it is not already running.  Does
    other useful things like keeping the daemon from dumping
    core if it terminates unexpectedly.

    --check <name>:
       Check that <name> is running, as opposed to simply the
       first argument passed to daemon().
    --user <username>:
       Run command as user <username>

With CentOS/RHEL 6, you also have the option of using an upstart job file instead of writing a sysv init script.

Related Question