我的斯特德去哪儿了? [英] Where's my stderr going?

查看:67
本文介绍了我的斯特德去哪儿了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下命令的bash脚本:

mv old.txt new.txt

I have a bash script that includes the command:
mv old.txt new.txt

Old.txt不存在,因此当我从命令行运行脚本时,它会给我以下消息:

mv:无法统计``test1。 txt':没有这样的文件或目录

Old.txt doesn't exist, so when I run the script from the commandline, it gives me the message:
mv: cannot stat ``test1.txt': No such file or directory

但是,如果我从cron调用脚本,请尝试将stderr和stdout重定向到日志文件,我最终得到一个完全空白的日志(在这种情况下,此操作是作为root用户完成的,但是在用户crontab中设置该日志时,我仍然遇到此问题)。

If I call the script from cron, however, and try to redirect stderr and stdout to a log file, I end up with a completely blank log (this is done as root in this case, but I still have this problem when it's set in my user crontab).

Crontab条目:

#mh dom mon dow命令

0 * * * * / home / beekguk / scripts / test.sh 2& 1>> /home/beekguk/logs/test.log

Crontab entry:
# m h dom mon dow command
0 * * * * /home/beekguk/scripts/test.sh 2>&1 >> /home/beekguk/logs/test.log

所以-在这种情况下stderr会去哪里?它会消失吗?我在各种论坛上都看到过将其邮寄到root,但是我不确定这意味着什么或如何找到它。

So - where's stderr going in this case? Does it just disappear? I've seen on various forums that it gets mailed to root, but I'm not sure what that means or how to find it.

我对Linux足够了解和bash使我陷入困境,我有一种感觉,结果可能是我sm着额头走了 Doh!,但我还是想尝试一下。

I know just enough about Linux and bash to get me in hot water, and I have a feeling this one may end up with me smacking my forehead and going "Doh!", but thought I'd give it a try anyway.

编辑:我通过更正crontab条目上的重定向来解决此问题:

0 * * * * /home/beekguk/scripts/test.sh >> /home/beekguk/logs/test.log 2>& 1
...但是我仍然很好奇,如果我忘记重定向它们,这些消息将流向何处!

I got around this by correcting the redirection on my crontab entry:
0 * * * * /home/beekguk/scripts/test.sh >> /home/beekguk/logs/test.log 2>&1 ... but I'm still curious about where these messages would go if I forgot to redirect them!

对于以后阅读此书的任何人:请注意,对于有用的评论主题,我同样接受答案。

For anyone reading this in the future: note that I accepted the answer equally for the helpful comment thread.

推荐答案

您的重定向已撤消。因此,标准错误将用于cron作业的标准输出,通常会将该邮件输出给拥有该作业的人。

You have your redirections reversed. Standard error is therefore going to the cron job's standard output, which is normally mailed to whoever owns the job.

这篇关于我的斯特德去哪儿了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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