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 startthe 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 statusit says it's running and lists its PID. I can see it inpsso it is running. - now when I run
service someprog stopit fails to stop. I can verify that it's still running withps.
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":
- Call to daemon in a /etc/init.d script is blocking, not running in background
- Getting shell script to run as a daemon on CentOS?
Edit: this answer on double-forking might have solved my problem, but now my program itself double-forks and that works: https://stackoverflow.com/a/9646251/898699