How to get a process exit status from another shell session?
Solution 1:
Use strace
as follows:
sudo strace -e trace=none -e signal=none -q -p $PID
Neither system calls nor signals are of interest here, so we tell strace
to ignore them with the -e
expressions and supress a status message with -q
. strace
attaches to the process with PID $PID
, waits for it to exit normally and outputs its exit status like this:
+++ exited with 0 +++
A simple if
expression to call any type of notification could be:
if sudo strace -e trace=none -e signal=none -q -p $PID |& grep -q ' 0 '; then
echo yeah
else
echo nope
fi
Example run
# in terminal 1
$ (echo $BASHPID;sleep 10;true)
8807
# in terminal 2
$ if sudo strace -e{trace,signal}=none -qp8807|&grep -q ' 0 ';then echo yeah;else echo nope;fi
yeah
# in terminal 1
$ (echo $BASHPID;sleep 10;false)
12285
# in terminal 2
$ if sudo strace -e{trace,signal}=none -qp12285|&grep -q ' 0 ';then echo yeah;else echo nope;fi
nope
Most of the credit goes to this answer on U&L, please leave an upvote there if you find this useful.