bash - linux script with netcat stops working after x hours -
i've have scripts:
#!/bin/bash netcat -lk -p 12345 | while read line match=$(echo $line | grep -c 'keep-alive') if [ $match -eq 1 ]; [start command] fi done
and
#!/bin/bash netcat -lk -p 12346 | while read line match=$(echo $line | grep -c 'keep-alive') if [ $match -eq 1 ]; [start command] fi done
i've put 2 scripts in '/etc/init.d/'
when restart linux machine (rasbpi), both scripts work fine.
i've tried them 20 times, , keep working fine.
but after around 12 hours, whole system stops working. i've put in loggin, seems scripts not reacting anymore. when i;
ps aux
i can see scripts still running:
root 1686 0.0 0.2 2740 1184 ? s aug12 0:00 /bin/bash /etc/init.d/script1.sh start root 1689 0.0 0.1 2268 512 ? s aug12 0:00 netcat -lk 12345 root 1690 0.0 0.1 2744 784 ? s aug12 0:00 /bin/bash /etc/init.d/script1.sh start root 1691 0.0 0.2 2740 1184 ? s aug12 0:00 /bin/bash /etc/init.d/script2.sh start root 1694 0.0 0.1 2268 512 ? s aug12 0:00 netcat -lk 12346 root 1695 0.0 0.1 2744 784 ? s aug12 0:00 /bin/bash /etc/init.d/script2.sh start
after reboot start working again... thats sin, rebooting linux machine periodically...
i've inserted loggin, here's outcome;
listening on [0.0.0.0] (family 0, port 12345) [2013-08-14 11:55:00] starting loop. [2013-08-14 11:55:00] starting netcat. netcat: address in use [2013-08-14 11:55:00] netcat has stopped or crashed. [2013-08-14 11:49:52] starting loop. [2013-08-14 11:49:52] starting netcat. listening on [0.0.0.0] (family 0, port 12345) connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6333) connection closed, listening again. connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6334) [2013-08-14 12:40:02] starting loop. [2013-08-14 12:40:02] starting netcat. netcat: address in use [2013-08-14 12:40:02] netcat has stopped or crashed. [2013-08-14 12:17:16] starting loop. [2013-08-14 12:17:16] starting netcat. listening on [0.0.0.0] (family 0, port 12345) connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6387) connection closed, listening again. connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6388) [2013-08-14 13:10:08] starting loop. [2013-08-14 13:10:08] starting netcat. netcat: address in use [2013-08-14 13:10:08] netcat has stopped or crashed. [2013-08-14 12:17:16] starting loop. [2013-08-14 12:17:16] starting netcat. listening on [0.0.0.0] (family 0, port 12345) connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6167) connection closed, listening again. connection [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6168)
thanks
about loop this.
#!/bin/bash (( ;; )) netcat -lk -p 12345 | while read line match=$(echo "$line" | grep -c 'keep-alive') if [ "$match" -eq 1 ]; [start command] fi done sleep 4s done
with added double quotes keep safer.
and try capturing errors , add logging format:
#!/bin/bash { echo "[$(date "+%f %t")] starting loop." (( ;; )) echo "[$(date "+%f %t")] starting netcat." netcat -lk -p 12345 | while read line match=$(echo "$line" | grep -c 'keep-alive') if [ "$match" -eq 1 ]; [start command] fi done echo "[$(date "+%f %t")] netcat has stopped or crashed." sleep 4s done } >> "/var/log/something.log" 2>&1
your read command better in format since read lines unmodified:
... | while ifs= read -r line
some suggest use of process substitution don't recommend time since through | while ...
method while
loop able run on subshell , keep outer for
loop safe in case crashes. besides there isn't variable while
loop needed outside of it.
i'm having idea issue might have been related input , how while read line; ...; done
block handles and not netcat itself. variables not being quoted around "" 1 of it, or actual reason why netcat crashing.
Comments
Post a Comment