Kill a child-script when it emits a certain string

35 views Asked by At

I have the following setup:

#!/bin/bash
# call.sh

voip_binary $1

Now when I a certain event happens (when I hang up), then the voip_binary emits a certain string: ... [DISCONNCTD] ... However, the script does not stop, but continues running. Now as soon as DISCONNCTD is discovered in the output, I want to kill the script.

So I could do the following, to get the relevant output:

voip_binary $1 | grep DISCONNCTD

Even though I now only get relevant output from the binary, I still don't know how to kill it, as soon as a line is emitted.

1

There are 1 answers

5
Oleg Andriyanov On

You can try this:

voip_binary $1 | grep 'DISCONNCTD' | while read line; do
    pkill 'voip_binary'
done

Assuming voip_binary outputs your string without buffering.