1

I want to use start-stop-daemon to stop the scripts it has started, but currently the scripts are not killed, and so I have resorted to hacking around it:

#!/bin/sh
case $1 in
 start)
  start-stop-daemon --start --background -c myapp --exec /home/myapp/dev-myapp.sh
 ;;
 stop)
  # couldn't get this to work  - hacking around it
  #start-stop-daemon --stop  -c myapp --exec /home/myapp/dev-myapp.sh

  # hack
  killall dev-myapp.sh
  sleep 3
  killall -9 dev-myapp.sh

 ;;
 restart)
  $0 stop
  $0 start
 ;;
 *)
     echo "No such command. "
     echo "Usage: $0 start|stop|restart"
esac
exit 0

How can I get the script to kill the bash scripts it has started using start-stop-daemon?

edit: I assume the failure to stop the processes has to do with this section from the man page:

-x, --exec executable
      Check for processes that are instances of this executable. The executable argument should be an  absolute  pathname.  Note:  this
      might not work as intended with interpreted scripts, as the executable will point to the interpreter. Take into account processes
      running from inside a chroot will also be matched, so other match restrictions might be needed.

So I might be forced to rely on name detection instead, but I don't know what the process name is ... Is this the whole absolute filename, the filename alone, or something else?

oligofren
  • 20,744
  • 16
  • 93
  • 180

1 Answers1

1

Regarding your comment, if you're willing to extend the shellscript you are running, you can use a pidfile. In practice, maybe you want to make it an option to your script, but as an example, this line would be sufficient:

echo $$ >/var/run/dev-myapp.sh.pid

Then, use these matching parameters for start-stop-daemon, if necessary replacing /bin/bash with whatever shell executes your script:

-p /var/run/dev-myapp.sh.pid -x /bin/bash

(To clarify: The process name of the script is that of the script interpreter, in your case, the shell)

  • Thanks for that. I was trying to avoid the pid file as the scripts are run as a non-root user (sudo -u appuser myscript.sh), and so I would need to create a for-the-purpose temporary folder to host the pid files. Not the worst thing in the world, though :-) – oligofren Jun 21 '17 at 19:17
  • 1
    Well, just matching `/bin/bash` seems a bit dangerous and the only other option I could think of is to write a "wrapper" daemon in C that executes the script with a different uid ... sounds like a tiny bit too much work for the goal, but could probably be done in around 1 or 2 hours. –  Jun 21 '17 at 19:22
  • haha, definitively not what I had in mind. let's just be pragmatic. pid file it is. – oligofren Jun 21 '17 at 19:51