I'm trying to run a program with an ncurse interface as a daemon using screen. I want to use start-stop-daemon to manage the process, but I'm having trouble creating a SysV init script.
Variables:
NAME=rtorrent
CHDIR=/opt/$NAME
DAEMON=$NAME
DAEMON_ARGS="-d -m -S $NAME $DAEMON &> /dev/null"
USER=media
GROUP=media
PIDFILE=/var/run/$NAME.pid
Currently, my start function is this:
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
pgrep -F $PIDFILE > /dev/null 2>&1 || return 1
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec screen -- $DAEMON_ARGS || return 2
}
But this is storing the sleep
process ID. Conceivably, the daemon could go down and leave the sleep
process running
My stop function needs to stop both, so I have:
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
pgrep -F $PIDFILE > /dev/null 2>&1 || return 1
for i in `ps -C $NAME -o pid=` ; do kill $i ; done
pgrep -F $PIDFILE > /dev/null 2>&1 || return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
}
Which should work (not tested yet), but with the issue that it will bring down any other process created by any other user with $NAME
as in its name or arguments.
I think my solution is to have my ps
function return only pids of processes names $NAME
, that were created by $USER
. Since this daemon will be run under a dedicated userid.
I'm not sure how to get this output. ps -C $NAME -u $USER o pid=
gives me a list for each match, but I want one list for both matches. Incase I decide this user can handle some other process later.
Also, what about reloading?
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
will reload the screen
process, will it reload the daemon? Is there a better way?
Any help greatly appreciated.