I would like to start a tcpdump in my own script an after some time I would like to kill it again. How can I access the PID from the tcpdump. I tried it with $$ , but this only kills the script.
if [[ $TIMEEND != $Zeit ]];then
echo "tcpdump started"
sudo tcpdump -i eth0 -w /media/usbhd-sd[b-c]1/abfrage2.pcap &
pid1=$!
break
#sudo umount /dev/sdb1
else
sudo kill $pid1
echo "autodump stopped"
fi
This is the output when i grep tcpdump so I think it's not possible to use pidof and pkill.
I tried to do it with awk
pid1=$(ps -eo pid,args|awk '/abfrage2/ && ! /awk/{print $1}')
echo $pid1 >> /tmp/test.txt
sudo kill $pid1
When i directly use the command for pid, it finds the pid but it doesn't execute the kill.
I think I have to overthink my code a little bit. I inmport the time from the config file, then I check if the starttime is the same and if it is it should also check if it isn't the endtime. Then it should execute the tcpdump as long as it isn't the endtime. When it arrives the endtime it should kill the the tcpdump. I know it's not the best solution, but it would be nice to get it working with something like that.
#!/bin/sh
source /media/usbhd-sdb1/config.conf
pluggedin=true
echo $TIMESTART
echo $TIMEEND
echo $$
echo $Zeit
echo $$ >> /tmp/test.txt
while [ $pluggedin ];do
Zeit=$(date +"%T")
if [[ $TIMESTART == $Zeit ]];then
if [[ $TIMEEND != $Zeit ]];then
echo "tcpdump started"
sudo tcpdump -i eth0 -w /media/usbhd-sd[b-c]1/abfrage2.pcap &
#sudo umount /dev/sdb1
else
pid1=$(ps -eo pid,args|awk '/abfrage2/ && ! /awk/{print $1}')
echo $pid1 >> /tmp/test.txt
sudo kill -9 $pid1
echo "autodump stopped"
fi
else
echo "tcpdump not yet started"
fi
done
tcpdump
to run for you should consider thetimeout
command which will do what you want without you having to try to reinvent it all