How to capture a return code of a command in shell script


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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should use the popen system calls:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xewooxAuthor Commented:
I want to do this in a shell script and not in C/C++ program.

You need to capture it when you execute the script.  I would write another shell script called to call your first shell script as so:
if my_script ; then
         echo my_script exited correctly
         echo my_script exited with an error

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

This should redirect the echo statments of the new script to a text file.
xewooxAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.