[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

How to capture a return code of 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.
I have written a code to continue checking the progress and will terminate the process if I 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.

Thank you.

0
xewoox
Asked:
xewoox
  • 2
  • 2
2 Solutions
 
patrick_defibaugh_chavezCommented:
You should use the popen system calls:

http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html
0
 
xewooxAuthor Commented:
I want to do this in a shell script and not in C/C++ program.

0
 
patrick_defibaugh_chavezCommented:
You need to capture it when you execute the script.  I would write another shell script called print_exit_status.sh to call your first shell script as so:

print_exit_status.sh:
if my_script ; then
         echo my_script exited correctly
else
         echo my_script exited with an error
fi

Then call the new scirpt like so:
sh print_exit_status.sh > my_script_return_value.txt &

This should redirect the echo statments of the new script to a text file.
0
 
xewooxAuthor Commented:
Thank you
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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