2

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

case "$1" in
   start)
      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 ?

1
  • 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. Commented Aug 8, 2014 at 22:45

1 Answer 1

3

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

3
  • Thanks @jordanm for completing my answer. I forgot on the wait command. Commented Aug 9, 2014 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. Commented Aug 9, 2014 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. Commented Aug 9, 2014 at 14:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.