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

查看:128
本文介绍了重定向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'

这样做的结果是,我收到 ./脚本/ my_script.rb 的输出 ./登录/ my_log.log 。这种行为是需要的。什么是好奇的是,我也接受我的本地邮件的输出。我想知道如何我的脚本的输出被邮寄抓获。因为我将输出重定向到一个日志文件,我希望我的cron作业就没有输出,从而cron作业运行时我没有收到任何邮件。任何人都可以提供一些线索,以邮件是如何能够得到的输出 ./脚本/ 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?

推荐答案

您重定向顺序不正确。标准错误没有被重定向到文件,但被发送到标准输出。这就是你必须在你的邮件被接受的。

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天全站免登陆