Bash 重定向在 Cron 中不起作用

sbt*_*jbq 3 bash cron

我试图&>>在我的 cron 作业中重定向 STDOUT 和 STDERR,但我在日志文件中没有看到任何内容。该文件在第一次运行时创建,但没有 OUT 或 ERR。

当我直接运行带有重定向的命令时&>>,它按预期工作。

我可以使用 成功重定向输出... >> /tmp/log 2>&1,但根据此 GNU 页面 (3.6.5),这两种方法是等效的,所以我不确定为什么第一种方法不起作用。

我在这里缺少什么想法吗?

代码示例:

#
# test.bash
#
echo "OUT"
ecko "ERR"
Run Code Online (Sandbox Code Playgroud)
#
# crontab
#
* * * * * /bin/bash /root/test.bash &>> /tmp/test_log_1      # NOT WORKING
* * * * * /bin/bash /root/test.bash >> /tmp/test_log_2 2>&1  # WORKING
Run Code Online (Sandbox Code Playgroud)
#
# crontab
#
* * * * * /bin/bash /root/test.bash &>> /tmp/test_log_1      # NOT WORKING
* * * * * /bin/bash /root/test.bash >> /tmp/test_log_2 2>&1  # WORKING
Run Code Online (Sandbox Code Playgroud)

系统详细信息:

  • Ubuntu 20.04.4 LTS
  • 重击 5.0.17
  • 克朗3.0pl1-136ubuntu1

ste*_*ver 5

/bin/shCron默认使用不支持重定向语法来解释作业条目&>>

如果您希望作业在不同的 shell 中运行,您可以SHELL在 crontab 中设置变量:

SHELL=/bin/bash
* * * * * /bin/bash /root/test.bash &>> /tmp/test_log_1
Run Code Online (Sandbox Code Playgroud)

man 5 crontab详情请参阅。