Search code examples
bashoutputdaemon

start-stop-daemon alway returns success


I have the following piece of code in a BASH script:

 start-stop-daemon --start --quiet --background \
    --startas /bin/bash -- -c "$CMD; exec echo $?> $FILE"

where CMD is some command string and FILE is an output file location. For some reason though, the output in FILE is always 0, even when it should give a different number (1 for error, etc).

Why is that, and how do I fix it?


Solution

  • Your $? gets expanded in the context of the current shell together with $CMD and $FILE.

    To fix this you'll have to escape it like \$?, i.e.

    ...  /bin/bash -c "$CMD; exec echo \$? >$FILE"