带有 netcat 的 linux 脚本在 x 小时后停止工作

标签 linux bash raspberry-pi netcat

我必须编写脚本:

#!/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/'

当我重新启动我的 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 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 机器……

我已经插入了一些登录,这是结果;

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)。实际上,由于您正在运行守护进程,因此您应该确保您的任何命令都不会从它(终端)读取输入。

#!/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"

我认为我们可以再次尝试日志记录:

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 小时后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105299/

相关文章:

bash - 语法错误 : end of file unexpected (expecting "fi")

bash - .vimrc 关闭 Action

java - Log4J 看起来不适合与 sudo 一起使用

c - 前向声明 C

linux - 计算文件夹中所有文件的总行数

linux - POSIX 将用户输入可接受的字符数限制为 4096,如何增加?

java - 在 Java 中使用 smartcardio 在 Raspberry Pi 上使用多个 NFC ACR122U 设备

cross-compiling - chroot 一个 scratchbox2 session ?

python - 在 Raspberry PI 上用 Python 执行 FFmpeg 命令

c++ - getppid() 不返回 parent 的 pid