我試圖用我的腳本啓動一個tcpdump。但我總是得到不止一個。這是我的ps的輸出:命令在bash腳本中多次啓動
1260 root tcpdump -i br0 -C 1024 -w /media/usbhd-sdc2/pcaps/abfrage2.pcap
1267 root tcpdump -i br0 -C 1024 -w /media/usbhd-sdc2/pcaps/abfrage2.pcap
這是我的代碼,我檢查,如果正常運行時間就像是我的開始時間相同。 如果它是相同的,它應該啓動一個tcpdump,它也應該保存PID。
但是爲什麼它會啓動第二個,這是否發生,因爲我的函數readPID速度不夠快?
if [[ $timestart == $Zeit ]] || [[ "$pid1" != "" ]];then
echo "First"
if [[ $timeend != $Zeit ]];then
echo "second"
if [ "$pid1" == "" ];then
echo "third"
if [ "$port" != "" ];then
echo "fourth"
run_tcpdump port
pid1=$(readPID1)
echo $pid1
else
ReadPID funtion:
readPID1(){
ps -eo pid,args|awk '/abfrage2/ && ! /awk/{print $1}'
}
更新
tcpdump的功能:
run_tcpdump(){
if [ "$1" == "port" ];then
sudo tcpdump port $port -i br0 -C 1024 -w /media/usbhd-sd[b-c]2/pcaps/abfrage2.pcap &
else
sudo tcpdump -i br0 -C 1024 -w /media/usbhd-sd[b-c]2/pcaps/abfrage2.pcap &
fi
}
命名一個實用程序(你也運行)後的shell函數罷工我只是*乞求*的驚喜... – Jens
@Jens改變了函數的名稱,實際上沒有想到的,但它仍然doesn沒有工作。 – aha364636
我可以再次猜測,您可能會在某處再次調用此函數。或者稱這個腳本兩次。 – baldr