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.

Sign up to request clarification or add additional context in comments.

1 Comment

But not when it was put in the background. It's easy to miss the & in the original question.
22
(/bin/sh -c "myscript.sh" >log.txt 2>&1 ; echo $? >somefile) & echo $!

Comments

-1
(
/bin/sh -c 'myscript.sh` >log.txt 2>&1
echo $? > some_file
) &

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.