aha364636
aha364636

Reputation: 363

Command is started more than once in bash script

I'm trying to start one tcpdump in with my script. But I always get more than one. That's my ps output:

 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

That's my code where I check if the uptime is the same like my start-time. If it's the same, it should start a tcpdump and it should also save the PID.
But why does it start a second one, does this happen, because my function readPID isn't fast enough?

 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}'
}

Update

Tcpdump function:

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
}

Upvotes: 0

Views: 84

Answers (1)

baldr
baldr

Reputation: 2999

I guess you might call your tcpdump function instead of system-wide package. rename it in your script to run_tcpdump for example. Not sure it is the root cause though.

Also make sure you have no tcpdump processes before you've started your script. Are you sure first process was not from previous run?

Upvotes: 1

Related Questions