How can I catch the exit code of a killed background process in a Bash script?

April 20, 2025 3 answers
Asked by !kern75
Hey everyone! I'm currently working on a project and ran into a bit of a snag. I'm trying to catch the exit code (status code) of a background process. For example, I have a program that exits with code 99 when it receives a SIGINT. Here's the relevant code for the program: ```c #include #include #include void bye() { // exit with code 99 if sigint was received exit(99); } int main(int argc,char** argv) { signal(SIGINT, bye); while(1) { sleep(1); } return 0; } ``` After compiling this program with `gcc example.c -o byeprogram`, I wrote a Bash script to run it in the background: ```bash set -x __do_before_wait() { ##some commands return 0 } __do_after_trap() { ##some commands return 0 } runbg() { local __start_time __finish_time __run_time __start_time=$(date +%s.%N) # Run the command in the background ($@) & __pid=$! trap ' kill -2 $__pid echo $? __finish_time=$(date +%s.%N) __run_time=$(echo "$__finish_time - $__start_time" | bc -l) echo "$__run_time" __do_after_trap || exit 2 ' SIGINT __do_before_wait || exit 1 wait $__pid } out=$(runbg /path/to/byeprogram) ``` But here's the problem: I want to be able to catch and print the exit code 99, but I can't seem to do it. When I manually run `byeprogram` and press Ctrl+C in the terminal, it correctly returns 99. How can I capture this 99 status code in my script?

Answers

glossary.PAGE
If you're really struggling, remember that when you run the program directly in the terminal and do `echo $?` after pressing Ctrl+C, it shows 99. This means the shell knows the exit code for the killed program, so it must be a script execution issue. Keep tinkering with it!
Reply to this answer
~call it even
You can actually catch the exit status by adding a `wait` inside your trap function. It will give you the exit status (99) when you do it that way. Just make sure that your `runbg` command isnโ€™t inside `$()` when you call it, or the trap wonโ€™t work in the parent shell when you hit Ctrl+C.

~catalogue entry~: Thanks for the tip! I added `wait $__pid` after `kill -2 $__pid`, and it does return the exit code. But Iโ€™m running into another issueโ€”my trap code ends with `echo $__status_code,$__pid`, but it prints nothing when I try it with `set -x`. Any ideas?

bead.by.bead: The same thing happened to me! It might be worth trying to create a nested function for the trap inside `runbg`. That way, it might correctly capture and print the exit status for you.

Reply to this answer
Clinker
In your `runbg` function, make sure you're using `wait pid` to get the exit code. This method will successfully return the exit status when the background process ends, including when it's killed. Give it a go, and you should see results!

chord_maj7: I tried that out, and it worked like a charm! Appreciate the suggestion!

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions