【问题标题】:Why this Debian-Linux autostart netcat script won't autostart?为什么这个 Debian-Linux 自动启动 netcat 脚本不会自动启动?
【发布时间】:2014-11-18 09:40:37
【问题描述】:

我在 rc.local 中放置了一个指向我的脚本的链接,以便在 linux debian 启动时自动启动它。它开始然后在 while 循环处停止。这是一个 netcat 脚本,永久监听 4001 端口。

echo "Start"

while read -r line
do

    #some stuff to do

done < <(nc -l -p 4001)

当我使用命令 ./myscript 以 root 身份启动此脚本时,它可以 100% 正确运行。需要 nc (netcat) 根级访问权限或其他什么?

编辑:

rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/etc/samba/SQLScripts
exit 0

rc.local 启动我的脚本“SQLScripts”

SQLScripts

#! /bin/sh

# The following part always gets executed.
echo "Starting SQL Scripts" >> /var/log/SQLScriptsStart
/etc/samba/PLCCheck >> /var/log/PLCCheck &

“SQLScripts”启动“PLCCheck”(仅举例)

PLCCheck

#!/bin/bash

echo "before SLEEP" >> /var/log/PLCCheck
sleep 5
echo "after SLEEP" >> /var/log/PLCCheck

echo "vor While" >> /var/log/PLCCheck

while read -r line
do

    echo "in While" >> /var/log/PLCCheck

done < <(netcat -u -l -p  6001)

【问题讨论】:

    标签: linux bash debian netcat


    【解决方案1】:

    在 rc 脚本中,默认情况下您具有 root 级别的访问权限。 “它在while循环处停止”是什么意思?一段时间后它退出了,还是这样?我猜你需要在后台运行你的循环才能实现自动启动脚本中常见的功能:

    echo "Starting"
    
    ( while read -r line
    do
    
        #some stuff to do
    
    done << (nc -l -p 4001) ) &
    
    echo "Started with pid $( jobs -p )"
    

    【讨论】:

    • 我将所有回声都写入日志文件。当我重新启动时,我的日志文件如下所示: Start 当我查看我的进程时 myscript 没有出现。所以我认为它已经停止了。
    • 仍然... rc 脚本应该尽快终止。它们旨在启动在后台运行的进程,因此内部可能存在无限循环显然是一个坏主意。你试过我的模组吗?
    • 是的。我已经尝试过您的解决方案,但它不起作用。查看我上面的帖子,了解我对脚本的更改。
    【解决方案2】:

    我昨天测试了大致相同的东西,我发现您可以绕过系统并使用以下 crontask 执行您的 netcat 脚本。 :

    (每分钟,但您可以随意调整。)

    * * * * * /home/kali/script-netcat.sh // working for me 
    
    @reboot /home/kali/script-netcat.sh // this is blocked by the system. 
    

    据我所知,我认为默认情况下 debian(可能还有其他 linux 发行版)会阻止每个尝试执行 netcat 命令的脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 2016-02-01
      • 2021-04-07
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多