0

I am writing bash script and have the situations when process finishes with error before wait in the following example code:

./process <params> &
PID=$!
wait $PID

And wait $PID gives me zero. How can i get the real exit code of background process in this situation?

Thanks in advance!

hgfdd
  • 3
  • 1
  • It works as expected, `./process` exited with zero. Zero is the "real exit code of background process in this situation". Substitute `./process ` for `exit ` and you can test it, ex. `exit 42 & PID=$! ; wait $PID ; echo $?` will print `42`. – KamilCuk Sep 26 '19 at 10:10

1 Answers1

0

The real exit code is stored in $? after wait finishes. Therefore, 0 is already the correct exit code.

#! /bin/bash

function _exit_0 {
        sleep 2
        exit 0
}

function _exit_1 {
        sleep 2
        exit 1
}

_exit_0 &
wait $!
echo $?

_exit_1 &
wait $!
echo $?
exit 0

Output:

$ ./tmp.sh 
0
1
Bayou
  • 3,293
  • 1
  • 9
  • 22