简体   繁体   English

如何添加无限nc(netcat-openbsd)作业作为crontab -e命令以运行@restart?

[英]How do I add an infinite nc (netcat-openbsd) job as a crontab -e command to run @restart?

The following works fine from a default ubuntu terminal: 在默认的ubuntu终端上,以下工作正常:

    nc -P ANONYMOUS 10.0.1.2 7700 >> ~/some.log

I use that command to make one computer to connect to a remote server and sit there listening to information being sent to it by the remote server. 我使用该命令使一台计算机连接到远程服务器,然后坐在那里,侦听远程服务器发送给它的信息。 The length of time is infinite, and lines get added to the log every few seconds. 时间的长度是无限的,并且每隔几秒钟就会将行添加到日志中。

But as soon as I try to add this command as a cron job with crontab -e , it DOES NOT work! 但是,一旦我尝试使用crontab -e将此命令添加为cron作业,它就不起作用了!

(I really want cron to launch the command @restart ) (我真的很想cron启动命令@restart

I have isolated the problem somewhat: 我已经隔离了一些问题:

It appears that the connection is initially getting established, but then is closing immediately. 看起来该连接最初是建立的,但随后立即关闭。

When I add "-q 60" to the command: 当我在命令中添加"-q 60"

 nc -q 60 -P ANONYMOUS 10.0.1.2 7700 >> ~/some.log

The cronjob does in fact open the connection, and listens, for 60 seconds as specified (yay!), but when I change that to "-q -1" , to specify an infinite time period, the connection closes immediately (boo!). cronjob实际上确实打开了连接,并按照指定的方式监听了60秒(是!),但是当我将其更改为"-q -1"来指定无限的时间段时,连接立即关闭(boo!)。 。

The man page explicitly states for -q : " -q after EOF on stdin , wait the specified number of seconds and then quit. If seconds is negative, wait forever." 手册页明确声明了-q :“在stdin EOF后的-q,等待指定的秒数,然后退出。如果秒为负数,则永远等待。”

I believe this problem may have to do with the default behavior of the -q option, as I have found a related bug report/fix here: 我相信这个问题可能与-q选项的默认行为有关,因为我在这里找到了相关的错误报告/修复:

 https://bugs.launchpad.net/ubuntu/+source/netcat-openbsd/+bug/544935 

in which the issue may have been addressed for terminals with a prompt, but perhaps not for terminals without a prompt? 其中可能已经针对具有提示的终端解决了该问题,但可能没有针对没有提示的终端解决了该问题?

It might also be an independent issue, having just to do with passing in a negative value to the nc -q option. 这也可能是一个独立的问题,与将负值传递给nc -q选项有关。

Another possibility is that it may even be related to the cron shell, however, other tests I have tried, seem to indicate that cron (sh)ell is working as expected. 另一种可能性是,它甚至可能与cron外壳有关,但是,我尝试过的其他测试似乎表明cron(sh)ell正在按预期方式工作。

Note, I tried using various combinations of shells and environments when defining the crontab command, ( env -i sh; and bash; and sourcing various .bashrc files. Setting $PS1 to ensure line 5 in the default .bashrc file does not return before the file is sourced completely.). 注意,在定义crontab命令(( env -i sh;bash;以及采购各种.bashrc文件)时,我尝试使用各种外壳和环境组合。设置$PS1以确保默认.bashrc文件中的第5行不会在返回之前该文件是完全来源的。)。

I'm running: Ubuntu version 12.04.3 LTS 我正在运行: Ubuntu 12.04.3 LTS版本
Release: 12.04 netcat-openbsd: 1.89-4ubuntu1 发行: 12.04 netcat-openbsd: 1.89-4ubuntu1

So my questions are these: Why does adding the "-q -1" not working in this case? 所以我的问题是:在这种情况下,为什么不添加"-q -1" and: 和:

What other alternatives do I have to accomplish this task? 我还必须完成其他哪些选择?

try adding the '-d' option to netcat: 尝试将'-d'选项添加到netcat:

nc -d -q -1 -P ANONYMOUS 10.0.1.2 7700 >> ~/some.log

It worked for me. 它为我工作。 As per the man page the -d flag is "Do not attempt to read from stdin." 按照手册页,-d标志为“请勿尝试从stdin读取”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM