[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Wait for process in pipeline returns wrong exit code
From: |
Dave Jennings |
Subject: |
Wait for process in pipeline returns wrong exit code |
Date: |
Tue, 1 Jun 2021 20:18:09 +1000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 |
Hello all,
I'm having trouble with getting the exit code of a particular process
from a pipeline when waiting on it's process id.
I create this test script (wait-pid):
#!/bin/bash
timeout 5 sleep 10 &
parent_pid=$( ps --pid $! -o ppid --no-headers )
timeout_pid=$( pgrep --parent $parent_pid timeout )
wait $timeout_pid
echo "exit code: $?"
Running this results in:
exit code: 124
which is what I expect, timeout kills the sleep process and exits with
124, which wait dutifully returns.
But if I add a pipeline to the initial command:
#!/bin/bash
timeout 5 sleep 10 | cat &
parent_pid=$( ps --pid $! -o ppid --no-headers )
timeout_pid=$( pgrep --parent $parent_pid timeout )
wait $timeout_pid
echo "exit code: $?"
now I get:
exit code: 0
To double-check I tried this line:
$ timeout 5 sleep 10 | sh -c 'cat; exit 17' &
and get exit code of (you guessed) 17.
So this is the exit code of the whole pipeline, not the timeout command.
Shouldn't the exit code be that of the timeout command?
This is with bash 4.4.20(1)-release.
- Wait for process in pipeline returns wrong exit code,
Dave Jennings <=