[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

How to capture a return code from calling a command in shell script

Hi,

I have written a shell script to execute a command as

....
....
"$@" > /dev/null &
....
....
....

Now, this command will run in the background. It will run for a while before it will terminate.
This shell script will continue checking the progress and will terminate the process if it is need to.

Now the question is... if the command execute successfully, how do I check the return code from the command? What I need to do to get this information inside this shell script?

Thank you.
0
xewoox
Asked:
xewoox
2 Solutions
 
ozoCommented:
("$@" ; echo $? > status ) > /dev/null &
0
 
omarfaridCommented:
If you run a command / script in the background, then you can get it's process id from

$!

To get the exit status, you can do

wait $!
echo $?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now