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

Posted on 2008-11-03
Last Modified: 2010-04-20

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.
Question by:xewoox
    LVL 84

    Accepted Solution

    ("$@" ; echo $? > status ) > /dev/null &
    LVL 40

    Assisted Solution

    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 $?

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
    Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now