0

For example:

printstuffslowly | ts -s &
ARECORD_PID=$!

or:

arecord [options] > ~/recording.wav 2> >(ts -s > ~/errors.log) &
ARECORD_PID=$!

From my tests on the command line, $ARECORD_PID ends up with ts's PID in it. But what I think I want is printstuffslowly's PID or arecord's PID so that I can loop some more commands until it exits:

while kill -0 "$ARECORD_PID" > /dev/null 2>&1
do
        sleep 1
        sync
done

Is there a way to get that? Or does ts's PID actually work just as well for my purpose?

  • It turns out that my two examples are different. The first one gets ts's PID as I mentioned above, while a reformat of the same command to printstuffslowly > >(ts -s) & gets printstuffslowly's PID as muru says. – AaronD Jan 19 '17 at 5:25
3

It will still be the PID of arecord. From the bash manual:

($!) Expands to the process ID of the job most recently placed into the background, whether executed as an asynchronous command or using the bg builtin

The command in process substitution needs to be started before the backgrounded command:

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.

So that PID is essentially lost (you can still pgrep it, but no bash builtin command or variable will get it).

  • Okay, there's a lot going on there apparently, because my two examples (edited just before your answer) turned out to work differently. The one I'm concerned about though looks like exactly what I'm looking for. – AaronD Jan 19 '17 at 5:20
  • @AaronD pipes and process substitutions are different. Obviously the last command in the pipe is the last command backgrounded. I don't know why you want to use it when you only want to process stderr and not stdout. – muru Jan 19 '17 at 5:25
  • @AaronD in any case, it shouldn't matter whether you use the PID of arecord or ts when using pipes - if arecord dies, ts will also die because the pipe is closed when arecord dies. – muru Jan 19 '17 at 5:26
  • I was just going for a more general example because I figured that since I didn't know about process substitution before then not everyone else would either. And I ended up confusing myself even more. – AaronD Jan 19 '17 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.