21

I'm trying to control a python based program (which doesn't detach itself from console)

#!/bin/bash

user=nobody
pid=/var/run/xx.pid
name=xx
prog=/xx.py

case $1 in
    start)
        /sbin/start-stop-daemon --start -b --oknodo --user "$user" --name "$name" --pidfile "$pid" --startas "$prog" --chuid nobody -- --daemon
        ;;
    stop)
        /sbin/start-stop-daemon --stop --oknodo --user "$user" --name "$name" --pidfile "$pid" --retry=TERM/5/KILL/1
        ;;
    restart)
        ;;
    *)
        ;;
esac

The start part works fine. I can see the script up and running, but the stop part doesn't. It simply says No xx found running; none killed.

So I guess there's something wrong with the start part?

1 Answer 1

25

start-stop-daemon --start --pidfile "$pid" doesn't write to the pid file unless --make-pidfile (-m) is specified. Without --make-pidfile it is up to the program being launched to create it. Also for --make-pidfile to work, the process being launched can't daemonize itself (via a fork), as then start-stop-daemon won't know what PID it should put in the file.

The only thing --pidfile "$pid" does in your usage scenario is that it will result in start-stop-daemon not starting the program if it is already running.


If process still is not stopping, all the criteria passed to start-stop-daemon --stop must match. Meaning $pid has to be a running process, the UID of the process has to match $user, and the process name (arg0) has to match $name.
You can determine the value of arg0 by doing ps h -p $pid -o comm

5
  • Okay the pidfile is written. But stop part still not functioning, same error message
    – daisy
    Commented Jun 17, 2014 at 2:20
  • @warl0ck is the pid file correct, and does the UID of the process match $user?
    – phemmer
    Commented Jun 17, 2014 at 2:22
  • Yes, I saw start-stop-daemon tried to read /proc/pid/stat: read(4, "5559 (python) S 1 5558 5558 0 -1"..., 1024) = 326, so the name should be python instead perhaps?
    – daisy
    Commented Jun 17, 2014 at 2:24
  • 1
    Done, substituted name to python and it worked ;-P
    – daisy
    Commented Jun 17, 2014 at 2:25
  • Right, yes, $name has to match too. Completely missed that you were passing that in. Will update the answer.
    – phemmer
    Commented Jun 17, 2014 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .