重定向 cron 作业的输出 [英] Redirecting the output of a cron job

查看:26
本文介绍了重定向 cron 作业的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 crontab 中有以下条目:

I have the following entry in crontab:

0 5 * * * /bin/bash -l -c 'export RAILS_ENV=my_env; cd /my_folder; ./script/my_script.rb 2>&1 > ./log/my_log.log'

这样做的结果是我在./log/my_log.log 中接收到./script/my_script.rb 的输出.这种行为是需要的.奇怪的是,我也在本地邮件中收到了输出.我想知道我的脚本的输出是如何被邮件捕获的.由于我将输出重定向到日志文件,因此我希望我的 cron 作业没有输出,因此在 cron 作业运行时我不会收到任何邮件.任何人都可以阐明邮件如何能够获得 ./script/my_script.rb 的输出?

The result of this is that I am receiving the output of ./script/my_script.rb in ./log/my_log.log. This behavior is desired. What is curious is that I am also receiving the output in my local mail. I am wondering how the output of my script is being captured by mail. Since I am redirecting the output to a log file, I would expect that my cron job would have no output, and thus I would receive no mail when the cron job runs. Can anyone shed some light as to how mail is able to get the output of ./script/my_script.rb?

推荐答案

您的重定向顺序不正确.Stderr 没有被重定向到文件,而是被发送到 stdout.这就是您在邮件中必须收到的内容.

Your redirection order is incorrect. Stderr is not being redirected to the file, but is being sent to stdout. That's what you must be receiving in your mail.

通过将您的 cron 作业更改为以下内容来修复重定向:

Fix the redirection by changing your cron job to:

0 5 * * * /bin/bash -l -c
'export RAILS_ENV=my_env;
cd /my_folder;
./script/my_script.rb > ./log/my_log.log 2>&1'

这篇关于重定向 cron 作业的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆