I have a Shell script that starts a few background processes (using &
) and are automatically killed when the user calls Ctrl+C (using trap
). This works well:
#!/bin/sh
trap "exit" INT TERM ERR
trap "kill 0" EXIT
command1 &
command2 &
command3 &
wait
Now I would like to filter the output of command3 with a grep -v "127.0.0.1"
to exclude all the line with 127.0.0.1
. Like this:
#!/bin/sh
trap "exit" INT TERM ERR
trap "kill 0" EXIT
command1 &
command2 &
command3 | grep -v "127.0.0.1" &
wait
The problem is that the signal Ctrl+C doesn't kill command3 anymore.
Is there a way to capture pipe command3
with the grep
in order to be able to kill at the end of the process?
Thanks
question from:
https://stackoverflow.com/questions/65846776/how-to-pipe-background-processes-in-a-shell-script 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…