Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get exit status of a process in bash

Tags:

c++

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 gives 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.

like image 857
SeasonalShot Avatar asked Apr 25 '16 09:04

SeasonalShot


People also ask

How do you check exit status in bash?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.

How do you check the exit code?

Extracting the elusive exit code To display the exit code for the last command you ran on the command line, use the following command: $ echo $? The displayed response contains no pomp or circumstance. It's simply a number.

Is exit status of last command?

variable in Linux. “$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred.


2 Answers

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.

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

echo

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

#  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.

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

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

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

[1]+  Done         ./foo.sh

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

More info @ http://tldp.org/LDP/abs/html/exit-status.html

like image 107
Inian Avatar answered Sep 29 '22 01:09

Inian


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
like image 43
SaintHax Avatar answered Sep 28 '22 23:09

SaintHax