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