Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question
    
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 at 22:45

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
Thanks @jordanm for completing my answer. I forgot on the wait command. –  Chemik Aug 9 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 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 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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