与netcat的linux的脚本将停止X小时后工作 [英] linux script with netcat stops working after x hours
问题描述
我已经有脚本:
#!/bin/bash
netcat -lk -p 12345 | while read line
do
match=$(echo $line | grep -c 'Keep-Alive')
if [ $match -eq 1 ]; then
[start a command]
fi
done
和
#!/bin/bash
netcat -lk -p 12346 | while read line
do
match=$(echo $line | grep -c 'Keep-Alive')
if [ $match -eq 1 ]; then
[start a command]
fi
done
我已经把两个脚本在'的/etc/init.d /
I've put the two scripts in the '/etc/init.d/'
当我重新启动我的Linux机器(RasbPi),这两个脚本做工精细。
When I restart my Linux machine (RasbPi), both the scripts work fine.
我已经试过他们像20次,他们保持正常工作。
I've tried them like 20 times, and they keep working fine.
但是大约12小时后,整个系统停止工作。我已经把一些洛,但似乎脚本没有反应了。但是,当我;
But after around 12 hours, the whole system stops working. I've put in some loggin, but it seems that the scripts are not reacting anymore. But when I;
ps aux
我可以看到脚本仍在运行:
I can see that the scripts are 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
他们开始重新启动工作了......但是那一种罪过,定期重新启动一台Linux机器后...
After a reboot they start working again... But thats a sin, rebooting a Linux machine periodically...
我已经插入了一些洛,这里的结果;
I've inserted some loggin, here's the 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 already 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 from [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6333)
Connection closed, listening again.
Connection from [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 already 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 from [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6387)
Connection closed, listening again.
Connection from [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 already 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 from [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6167)
Connection closed, listening again.
Connection from [16.8.94.19] port 12345 [tcp/*] accepted (family 2, sport 6168)
感谢
推荐答案
如果您没有包含的命令的netcat从标准就可以完全使其运行独立于终端读取输入。有时后台进程仍然依赖于终端暂停(S),当他们试图从中读取输入的背景。实际上,因为你正在运行一个后台程序,你应该确保没有你的命令,从它(终端)读取输入。
If none of your commands including netcat reads input from stdin you can completely make it run independent of the terminal. Sometimes background process that are still dependent on the terminal pauses (S) when they try to read input from it on a background. Actually since you're running a daemon, you should make sure that none of your commands reads input from it (terminal).
#!/bin/bash
set +o monitor # Make sure job control is disabled.
(
: # Make sure the shell runs a subshell.
exec netcat -lk -p 12345 | while read line ## Use exec to overwrite the subshell.
do
match=$(echo $line | grep -c 'Keep-Alive')
if [ $match -eq 1 ]; then
[start a command]
fi
done
) <&- >&- 2>&- </dev/null &>/dev/null &
TASKPID=$!
sleep 1s ## Let the task initialize a bit before we disown it.
disown "$TASKPID"
和我想我们可以再次尝试登录的事情:
And I think we could try the logging thing again:
set +o monitor
(
echo "[$(date "+%F %T")] Starting loop with PID $BASHPID."
for (( ;; ))
do
echo "[$(date "+%F %T")] Starting netcat."
netcat -vv -lk -p 12345 | while read line
do
match=$(echo "$line" | grep -c 'Keep-Alive')
if [ "$match" -eq 1 ]; then
[start a command]
fi
done
echo "[$(date "+%F %T")] Netcat has stopped or crashed."
sleep 4s
done
) <&- >&- 2>&- </dev/null >> "/var/log/something.log" 2>&1 &
TASKPID=$!
sleep 1s
disown "$TASKPID"
这篇关于与netcat的linux的脚本将停止X小时后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!