What commands would I use to create two child processes serially

bashbash-scripting

I want to spawn two child processes, but wait until the first reaches a certain point in its execution (which can be determined by examining stdout) before spawning the second. I'm not sure which unix commands would be involved in accomplishing this, and have had a hard time finding anything via google due to the terse and sometimes cryptic nature of unix command names.

I'm not seeking a solution to the specific problem I'm trying to solve (although any additional pointers would be appreciated), I'm primarily concerned with knowing which unix commands I'd want to look up and learn to use.

Best Answer

You can use the xargs utility for this. It can run a command for each line on stdin, so we just need to ensure that xargs gets as input a single line on stdin at the time it should start the command, which can be accomplished with grep:

proc1 | grep --max-count=1 "${targetString}" | xargs --max-lines=1 --no-run-if-empty --replace -- proc2

The arguments --max-count=1, --max-lines=1 and --no-run-if-empty ensure that proc2 is started exactly once if and when proc1 outputs ${targetString} for the first time, and never if proc1 never outputs ${targetString}. The --replace avoids that xargs appends the input line to its command line.

I used the following command line to test it:

(echo Start >&2; sleep 3 ; echo Trigger; sleep 3; echo End >&2) | grep --max-count=1 Trigger | xargs --max-lines=1 --no-run-if-empty --replace -- echo "Triggered"
Related Question