SeasonalShot
SeasonalShot

Reputation: 2589

Get exit status of a process in bash

I have a C++ executable named Test. I want to find out the GetExitCodeProcess() code when the execution is completed.

I can find out the status code by writing the following in another wrapper program as such:

...
int status = system("./Test");

and then check for WIFSIGNALED / WIFSTOPPED etc statuses.

However, instead of writing a wrapper program, can I get the exit status code from the PID of the .Test program by writing a bash script?

Edit: Does writing a $ in the bash after executing ./Test give the solution to the above problem?

Edit: Summary is -- when I run an executable from the command line, how do I (not a program) get the exit status?

Upvotes: 55

Views: 65825

Answers (2)

Inian
Inian

Reputation: 85885

You can simply do a echo $? after executing the command/bash which will output the exit code of the program.

Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.

# Non-zero exit status returned -- command failed to execute.
echo $?    
  
echo

# Will return 113 to shell.
# To verify this, type "echo $?" after script terminates.    
exit 113             

# By convention, an 'exit 0' indicates success,
# while a non-zero exit value means an error or anomalous condition

Alternately if you wish to identify return code for a background process/script (started with nohup or run in background & operator) you could get the pid of the process/script started and wait for it to terminate and then get the exit code.

# Some random number for the process-id is returned
./foo.sh &
[1] 28992          

# Get process id of the background process
echo $!          
28992 

# Waits until the process determined by the number is complete
wait 28992       

[1]+  Done         ./foo.sh

# Prints the return code of the process
echo $?          
0

Also take a look at Bash Hackers Wiki - Exit Status

Upvotes: 67

SaintHax
SaintHax

Reputation: 1943

You are approaching this wrong-- system() already returns the exist status: zero for success, non-zero for failure. What is likely happening is your "Test" script is written incorrectly or you are testing "status" incorrectly. I've seen the following scripting mistakes from developers.

run_some_java
echo "Done"

At no time are they tracking the exit code, the script returns the last exit code-- of the echo command. Which makes it always successful. Here are two ways of exiting correctly (and I know other coders thing some more advanced ways are better than this first one, but I don't think that's what this thread is about). Crawl, then walk, then run.

If you need the exit status, but want all the shell commands to execute.

return_code=0

mv $letters $ARCHIVE
return_code=$(($return_code + $?))

mv $letters $ARCHIVE
return_code=$(($return_code + $?))

exit $return_code

If you want to exit on the failure, there is set -e to look into, and here is another way.

mv $letters $ARCHIVE || exit 1
mv $letters $ARCHIVE || exit 2

Upvotes: 8

Related Questions