Extending the question as suggested by Cyrus, I wanted to know if I could track the same script with PIPESTATUS
or something similar if I sent it to run in background?
bash script is as follows:
#! /bin/bash
{ python script.py 2>&1 | tee logfile.log; } &
ret="${PIPESTATUS[0]}"
if [[ "$ret" -ne "0" ]]; then
echo "$ret"
fi
and script.py
is:
print("hello")
exit(1);
print("world")
When I run the bash
script without &
it prints the PIPESTATUS
correctly but in case when I run it in background, no output is returned.
First off, as Kamil Cuk said, if you want the pipestatus of a background process, you need:
{ python script.py 2>&1 | tee logfile.log; exit "${PIPESTATUS[0]}" }
But since you're running something in the background, your if
statement will probably run before it's even completed, so what return value do you expect it to test?
What you typically want is to run something in the background, do a bunch of work, then wait
for the background task, and only then checking its return value.
wait
will return the exit status of the background shell, so that's the way to actually get the exit code generated by exit "${PIPESTATUS[0]}"