Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

#!/bin/bash
#
#   /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

prog="someprog"
exec="/usr/local/bin/$prog"
[ -e "/etc/sysconfig/$prog" ] && . "/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/$prog"
RETVAL=0

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

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

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

restart() {
    stop
    start
}   

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    status "$prog"
    RETVAL=$?
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|status}"
    RETVAL=2
esac

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: http://stackoverflow.com/a/9646251/898699

share|improve this question
    
Are you starting the program with the "daemon" utility provided by libslack. libslack.org/daemon/#documentation 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
2  
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
add comment

3 Answers

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
add comment

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/run.sh"

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
add comment
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. http://stackoverflow.com/a/9646251/898699

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

Your Answer

 
discard

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.