如何将stderr重定向到cron作业中的文件 [英] How to redirect stderr to a file in a cron job
问题描述
我的crontab中有一个像这样设置的cron作业:
I've got a cron job that is set up like this in my crontab:
*/1 * * * * sudo /home/pi/coup/sensor.py >> /home/pi/sensorLog.txt
它将stdout放入sensorLog.txt,并生成任何stderr收到一封电子邮件。
It puts stdout into sensorLog.txt, and any stderr it generates gets put into an email.
我希望stdout和stderr都进入sensorLog.txt,所以我在 1>& 2
中添加了crontab,它应该使stderr与stdout进入同一位置。现在看起来像这样:
I want both stdout and stderr to go into sensorLog.txt, so I added 1>&2
to the crontab, which is supposed to make stderr go into the same place as stdout. It now looks like this:
*/1 * * * * sudo /home/pi/coup/sensor.py >> /home/pi/sensorLog.txt 1>&2
现在,stdout和stderr都得到了放入电子邮件中,没有任何内容添加到文件中。这与我要完成的任务相反。
Now, both stdout and stderr both get put into an email, and nothing gets added to the file. This is the opposite of what I am trying to accomplish.
如何同时获得stdout和stderr才能重定向到文件?
How do I get both stdout and stderr to get redirected to the file?
推荐答案
反之亦然:
*/1 * * * * sudo /home/pi/coup/sensor.py >> /home/pi/sensorLog.txt 2>&1
2> & 1
会将标准错误(2)重定向到标准输出(1),该输出又被重定向到您的日志文件。因此,最后,stderr和stdout都将转到您的 sensorLog.txt
2>&1
will redirect standard error (2) to standard output (1) which -in turn - was redirected to your log file. So, at the end, both stderr and stdout will go to your sensorLog.txt
这篇关于如何将stderr重定向到cron作业中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!