无法在 cron 作业的脚本中记录 netcat 输出

Can not log netcat output from within script in a cron job

我有一个脚本: /home/blah/test.sh 包含以下行:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
netcat -v -w 5 169.254.123.123 xx >> /home/blah/output.txt

其中 xx 是端口号。

这个脚本 运行 在命令行中没有问题,脚本 运行 在通过 cronjob 调用时没有问题,但是 netcat 的输出除了“成功”之外是空的" 由详细标志输出到 netcat.

的消息

我已经比较了 PATH 变量,并且我还在 cron 条目之前在 crontab 文件中明确尝试了 SHELL=/bin/bash。我还使用 * * * * * env > /home/blah/env.blah 获得了 env 详细信息并尝试了:

env - `cat /home/blah/env.blah` /home/blah/test.sh

这行得通,但我无法让 netcat 的输出在 cronjob 中工作。 A which netcat returns /bin/netcat 所以我也尝试用 /bin/netcat 替换脚本中的 netcat 但这也不起作用。

我已经在 crontab 中为用户 blah 和 root -> 为 root 尝试过使用 sudo crontab -e 和编辑 /etc/crontab(根据需要明确指定用户名)。已尝试的 crontab 条目:

*/5 * * * * /home/blah/test.sh>>/home/blah/log.out 2>>/home/blah/log.err
*/5 * * * * /home/blah/test.sh
*/5 * * * * /home/blah/test.sh>>/home/blah/log.out >>/home/blah/log.err 2>&1

还有一些人

使用 Linux Mint 17.1(丽贝卡)。

如有任何想法,我们将不胜感激。

解决方案 -d netcat 需要标志才能 运行 从 cron 作业调用。

回答(详情在原文post的评论字符串中): 问题在于从 shell 调用 netcat 与从 cronjob 调用 netcat 的区别。在 cron 中没有 shell 实例,对此的解决方案是调用 -d 选项来抑制 netcat 对 shell.

的需求

感谢所有评论者的帮助!