I have set up start-stop-daemon to start my script automatically

case "$1" in
      log_begin_msg "starting foo"
      start-stop-daemon --start --chuid nobody --user nobody --pidfile \
      /tmp/foo.pid --startas /usr/local/bin/foo.sh &
      log_end_msg $?

the problem is, it always returns 0 (success),even if the process was not started.

How can I capture the return code of start-stop-daemon properly ?

  • it seems start-stop-deamon returns 0 if the requested action was performed (note that it's not on if the process started). You can add some logic and use pidof to match pid. – Simply_Me Aug 8 '14 at 22:45

You are not capturing return code of start-stop-daemon.

Your problem is that you are launching it in the background and it is started properly. I mean that you are capturing return code of starting something in background that wants to start something in background.

Try this:

rm /tmp/not_existent_file &
echo $?

This always prints 0.

In order to get the return code of a backgrounded process, you must wait for it to exit with wait. Here is an example:

rm /tmp/not_existent_file &
wait $!
echo $?

If you want to start process that is not forking on its own, try to use --background switch and remove & from the end of start-stop-daemon line.

See start-stop-deamon manpage

| improve this answer | |
  • Thanks @jordanm for completing my answer. I forgot on the wait command. – Chemik Aug 9 '14 at 0:06
  • when I add wait $! as suggested by you, then the start-stop-daemon never finishes. i.e. when I do service foo start it waits indefinitely. – Martin Vegter Aug 9 '14 at 9:26
  • Read the answer properly. The wait command is there only for illustration how to get return code of backgrounded process. Use --background switch instead. – Chemik Aug 9 '14 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.