bash 重定向 cron 作业的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14982980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirecting the output of a cron job
提问by Michael Frederick
I have the following entry in crontab:
我在 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'
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
?
结果是我收到了./script/my_script.rb
in的输出./log/my_log.log
。这种行为是需要的。奇怪的是,我也在本地邮件中收到了输出。我想知道我的脚本的输出是如何被邮件捕获的。由于我将输出重定向到日志文件,因此我希望我的 cron 作业没有输出,因此在 cron 作业运行时我不会收到任何邮件。任何人都可以阐明邮件如何获得./script/my_script.rb
?
回答by dogbane
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.
您的重定向顺序不正确。Stderr 没有被重定向到文件,而是被发送到 stdout。这就是您必须在邮件中收到的内容。
Fix the redirection by changing your cron job to:
通过将您的 cron 作业更改为以下内容来修复重定向:
0 5 * * * /bin/bash -l -c
'export RAILS_ENV=my_env;
cd /my_folder;
./script/my_script.rb > ./log/my_log.log 2>&1'
回答by Ilya I
Try swapping 2>&1
with > ./log/my_log.log
.
尝试2>&1
与> ./log/my_log.log
.
回答by Johannes
Judging by this answeryou just need to switch the order of the redirects:
从这个答案来看,您只需要切换重定向的顺序:
0 5 * * * /bin/bash -l -c 'export RAILS_ENV=my_env; cd /my_folder; ./script/my_script.rb > ./log/my_log.log 2>&1'