how to terminate a process via key stroke

Viewed 25

I have this function on my bash script:

sudo tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "dns.qry.name~." -q 1>>log.txt 2>/dev/null &
while true
do
cat log.txt
done

it is capturing ips and domain names in live mode and save them into log file. how can configure this live mode to be terminated by pressing a key?

1 Answers

Using tee to watch log and send the command to background, then read input to terminate script

tshark -i eth0 -T fields -e ip.src -e dns.qry.name -Y "ip" -q 2>/dev/null | tee log.txt  & 
read -n1 c && echo "Got key $c"
exit

Note: running the command in a console will terminate it :-p

Related