Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to install a program as a service in Red Hat. It doesn't background itself, manage its PID file, or manage its own logs. It just runs and prints to STDOUT and STDERR.

Using the standard init scripts as guides, I've developed the following:

#   /etc/rc.d/init.d/someprog
# Starts the someprog daemon
# chkconfig: 345 80 20
# description: the someprog daemon
# processname: someprog
# config: /etc/someprog.conf

# Source function library.
. /etc/rc.d/init.d/functions

[ -e "/etc/sysconfig/$prog" ] && . "/etc/sysconfig/$prog"

check() {
    [ `id -u` = 0 ] || exit 4
    test -x "$exec" || exit 5

start() {
    if [ ! -f "$lockfile" ]; then
        echo -n $"Starting $prog: " 
        daemon --user someproguser "$exec"
        [ $RETVAL -eq 0 ] && touch "$lockfile"
    return $RETVAL

stop() {
    echo -n $"Stopping $prog: "
    killproc "exec"
    [ $RETVAL -eq 0 ] && rm -f "$lockfile"
    return $RETVAL

restart() {

case "$1" in
    status "$prog"
    echo $"Usage: $0 {start|stop|restart|status}"

exit $RETVAL

It may be that my mistake was to copy-paste and modify some of the existing scripts in /etc/init.d. In any case, the resulting service behaves strangely:

  • when I start it with service someprog start the program prints to the terminal and the command doesn't complete.
  • if I CTRL-C, it prints "Session terminated, killing shell... ...killed. FAILED". I have to do this to get my shell prompt back again.
  • now when I run service someprog status it says it's running and lists its PID. I can see it in ps so it is running.
  • now when I run service someprog stop it fails to stop. I can verify that it's still running with ps.

What do I need to change so that someprog is sent to the background and managed as a service?

Edit: I have now found a couple of related questions, neither of them with an actual answer other than "do something else":

Edit: this answer on double-forking might have solved my problem, but now my program itself double-forks and that works:

share|improve this question
Are you starting the program with the "daemon" utility provided by libslack. In this case the program can be stopped as daemon -n name --stop. Also, try redirecting the output(when starting the program) to a file or /dev/null and check. – Ankit Mar 28 '13 at 4:45
Dependant on your version of redhat, you can just make a simple wrapper for it in upstart and call it in upstart directly. Then upstart will manage the service for you. This is an EL6 thing though. – Matthew Ife Mar 30 '13 at 20:54

If this is your program, please write it as a proper daemon. Especially if its for redistribution. :)

You might try monit. Or maybe something like runit or daemontools. Those mighty not have readily available packages. Daemontools is from DJB, if that influences your decision (in either direction.)

share|improve this answer

If your program cannot run as daemon, your should try to run it from screen:

/bin/su - username -c "/usr/bin/screen -dmS programscreenname /home/program/"

and you can return and see how it is doing using screen -r

share|improve this answer
Sorry, but this is not what I am looking for. As I said, I need the program to run as a service and be managed by the services framework. – Baron Schwartz Mar 28 '13 at 12:44
up vote -1 down vote accepted

I have done some more research and it appears that the answer is "you can't do that." The program to be run must actually daemonize itself properly: fork and detach its standard filehandles, detach from the terminal, and start a new session.

Edit: apparently I'm wrong -- double forking would work.

share|improve this answer
You can do that using upstart as stated in the comments. – user130370 Nov 4 '13 at 12:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.