Shell – Discard stdout of a command for t seconds

process-managementshell-scriptstdout

I am working on some batch scripts involving the following:

  • Run some non-terminating sub-processes (asynchronously)
  • Wait for t seconds
  • Perform other task X for some time
  • Terminate subprocesses

Ideally, I would like to be able to differentiate the stdout of the sub-processes which has been emitted before X from that which has been emitted after X.

A few ideas come to mind, although I have no idea as to how I would implement them:

  • Discard stdout for t seconds
  • Insert some text (for instance, 'Task X started') to visually separate the sections
  • Split stdout into various output streams

Best Answer

While you could complicate the matter with exec and extra file descriptor wrangling, your second suggestion is the simplest. Before starting X, echo a marker string into the log file.

All those commands would be appending to the same file, so maybe it would be a good idea to prepend all output of X with a marker, so you can tell apart its output from the one of the still running previous commands. Something along the lines of:

{ X; } | sed 's,^,[X say] ,'

This would make further analysis much simpler. It is not safe and for very verbose programs, race conditions would happen often.

If you're willing to take the chance to break one log line and can interrupt the first batch of apps without consequence, this would work too:

{ Y; } >> log &
sleep $t
kill -STOP %% # last job, like the same as %1
echo -e "\nX started" >> log
kill -CONT %%
{ X; } >> log2
Related Question