Jason Hu
Jason Hu

Reputation: 6333

how to suppress the reporting output from background tasks?

to clarify, by reporting output, here i mean the ones start with [1]:

$ echo hello world >&2 &
[1] 11714
hello world
[1]+  Done                    echo hello world 1>&2

which means i want hello world to be output.

i did a lot of search on this, and the solutions i found would be:

  1. having it run in a subshell
  2. set +m can deal with Done message only.
  3. suppress it explicitly: { cmd & } 2>/dev/null which won't suppress Done and it suppresses all my stderr too.

but in my condition, they don't work quite fine since i want extra parallelism. the framework should be:

cmd1 &>>log &
cmd2 &>>log &
wait
cat file &
cat file2 >&2 &
wait

if i put things into subshells, output is suppressed, but wait won't block the program.

the rest two options doesn't work as i've stated.

the worst is i am expecting something will be output to stderr. so i am looking for a way to totally suppress these reporting things or any other work around that you can come up with.

Upvotes: 1

Views: 688

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

This is very ugly but it looks like it works in a quick test.

set +m
{ { sleep 2; echo stdout; echo stderr >&2; } 2>&3- & } 3>&2 2>/dev/null

Create fd 3 as a copy of fd 2 then redirect fd 2 to /dev/null (to suppress the background id/pid message).

Then, for the backgrounded command list, move fd 3 back to fd 2 so things that try to use it go where you wanted them to.

My first attempt had the fd 3 mv in the outer brace command list but that didn't suppress the id/pid message correctly (I guess that happened too quickly or something).

Upvotes: 2

Related Questions