32

I'm trying to call a custom shell script through sh:

/bin/sh -c 'myscript.sh` >log.txt 2>&1 & echo $!

Output of this command is a PID of a created background process. I want to instruct /bin/sh to save return code of myscript.sh to some file. Is it possible?

3 Answers 3

40
echo $? >> /path/to/return_code

$? has the return code of the last statement in bash.

1
  • 8
    But not when it was put in the background. It's easy to miss the & in the original question.
    – Jens
    Commented Aug 26, 2011 at 18:51
22
(/bin/sh -c "myscript.sh" >log.txt 2>&1 ; echo $? >somefile) & echo $!
-1
(
/bin/sh -c 'myscript.sh` >log.txt 2>&1
echo $? > some_file
) &

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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