Tee resets exit status is always 0

Jeryl Vaz

I have a short script like this:

#!/bin/bash
<some_process> | tee -a /tmp/some.log  &
wait $(pidof <some_process_name>)
echo $?

The result is always 0, irrespective of the exit status of some_process.

I know PIPESTATUS can be used here, but why does tee break wait?

Yam Marcovic

Well, this is something that, for some peculiar reason, the docs don't mention. The code, however, does:

int wait_for (pid) { /*...*/
/* If this child is part of a job, then we are really waiting for the
job to finish. Otherwise, we are waiting for the child to finish. [...] */

if (job == NO_JOB)
  job = find_job (pid, 0, NULL);

So it's actually waiting for the whole job, which, as we know, normally yields the exit status of the last command in the chain.

To make matters worse, $PIPESTATUS can only be used with the last foreground command.

You can, however, utilize $PIPESTATUS in a subshell job, like this:

(<some_process> | tee -a /tmp/some.log; exit ${PIPESTATUS[0]}) &
# somewhere down the line:
wait %%<some_process>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exit status of tee>(...)

From Dev

Why is the exit status always 0 at the start of a script?

From Dev

grep always exits with 0 exit status in if statement

From Dev

Why is my GDB batch mode exit status always 0?

From Dev

Why is Gnu grep exit status always 0 on standard input?

From Dev

ss exit code always 0

From Dev

frmcmp exit status should not be 0

From Dev

supervisord always returns exit status 127 at WebFaction

From Dev

wait(status), WEXITSTATUS(status) always returns 0

From Dev

exited: scrapy (exit status 0; not expected)

From Dev

Bash / exit status is 0, while expected is 255

From Dev

Tool to run a command until exit status is 0

From Dev

tee always returns EINVAL

From Dev

xmlhttp request status always returns 0

From Dev

xmlhttp request status always returns 0

From Dev

xmlhttprequest status is always 0 and no response in responseText

From Dev

Why From resets to 0?

From Dev

HighScore resets to 0

From Dev

PowerShell exit code is always 0 in TeamCity 8 build step

From Dev

node js Exit status 8, error code 0 running on mac

From Dev

exit(1) in file results in script status code 0

From Dev

Bash: Loop until command exit status equals 0

From Dev

Bash does not remember programs with non 0 exit status in history

From Dev

Is checking for exit status other than 0 after a `command || return` unreachable?

From Dev

exit(1) in file results in script status code 0

From Dev

Ajax call always returns readystate 1 and status of 0

From Dev

Basic hello world server and Xmlhttprequest client -- Status always 0

From Dev

phonegap + iOS, http request always return status 0 on local files

From Dev

Does an init script always return a proper exit code when running status?

Related Related

HotTag

Archive