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

Posted on 2008-11-03
Last Modified: 2013-11-13

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.

Question by:xewoox
    LVL 1

    Accepted Solution

    You should use the popen system calls:

    Author Comment

    I want to do this in a shell script and not in C/C++ program.

    LVL 1

    Assisted Solution

    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.

    Author Comment

    Thank you

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    why to use string 10 53
    countHi challenge 25 62
    Specific format 21 109
    create an incrementing variable name AutoHotKey 5 31
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    732 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

    25 Experts available now in Live!

    Get 1:1 Help Now