Super User is a question and answer site for computer enthusiasts and power users. 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'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.


DAEMON_ARGS="-d -m -S $NAME $DAEMON &> /dev/null"

Currently, my start function is this:

    # 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:

    # 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.

share|improve this question

You must log in to answer this question.

Browse other questions tagged .