#!/bin/bashnetcat -lk -p 12345 | while read linedo match=$(echo $line | grep -c 'Keep-Alive') if [ $match -eq 1 ]; then [start a command] fidone
和
#!/bin/bashnetcat -lk -p 12346 | while read linedo match=$(echo $line | grep -c 'Keep-Alive') if [ $match -eq 1 ]; then [start a command] fidone
我把这两个脚本放在’/etc/init.d/’
当我重新启动我的linux机器(RasbPi)时,这两个脚本都可以正常工作.
我已经尝试了20次,他们继续工作正常.
但大约12个小时后,整个系统停止工作.我已经注册了一些登录,但似乎这些脚本不再起作用了.但是当我
ps aux
我可以看到脚本仍在运行:
root 1686 0.0 0.2 2740 1184 ? S Aug12 0:00 /bin/bash /etc/init.d/script1.sh startroot 1689 0.0 0.1 2268 512 ? S Aug12 0:00 netcat -lk 12345root 1690 0.0 0.1 2744 784 ? S Aug12 0:00 /bin/bash /etc/init.d/script1.sh startroot 1691 0.0 0.2 2740 1184 ? S Aug12 0:00 /bin/bash /etc/init.d/script2.sh startroot 1694 0.0 0.1 2268 512 ? S Aug12 0:00 netcat -lk 12346root 1695 0.0 0.1 2744 784 ? S Aug12 0:00 /bin/bash /etc/init.d/script2.sh start
重新启动后,他们再次开始工作…但这是一个罪,定期重新启动一台linux机器…
我已经插入了一些登录,这是结果;
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,sport 6387)Connection closed,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,sport 6167)Connection closed,sport 6168)
谢谢
解决方法 如果您的命令(包括netcat)都没有读取stdin的输入,您可以完全使其独立于终端运行.有时,依赖于终端的后台进程会在后台读取输入时暂停(S).实际上,由于你运行一个守护进程,你应该确保没有一个命令读取它的输入(终端).#!/bin/bashset +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"
而且我想我们可以再试一次记录:
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 1sdisown "$TASKPID"总结
以上是内存溢出为你收集整理的linux脚本与netcat在x小时后停止工作全部内容,希望文章能够帮你解决linux脚本与netcat在x小时后停止工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)