[英]How to redirect stderr to a file and some echos (not stdout) to the same file
I am learning bash scripting. 我正在学习bash脚本。 I am trying to write a simple script that runs
apt-get update
in regular intervals using crontab for scheduling. 我正在尝试编写一个简单的脚本,该脚本使用crontab进行定期运行
apt-get update
的调度。 I am trying to send the errors (with the time the error occurred )to a log file but I am only getting the error and not the time, 我正在尝试将错误(与错误发生的时间)发送到日志文件,但是我只是收到错误而不是时间,
The script file , 脚本文件
echo "---------- $(date +"%d %B %G") , $(date +%r) :";
apt-get update;
The crontab file (sudo crontab -e) crontab文件(sudo crontab -e)
0 */5 * * * /root/scripts/update_sys.sh 2>/root/logs/updateLog 1>/dev/null
as far as I understood the echo statement in the script file dumps the text to the standard output, thus it ends up in /dev/null
, but how could I include it in the log file without the entire info ( I mean info that apt-get
dumps like Hit http://in.archive.ubuntu.com precise-backports/main Sources
) ? 据我了解在脚本文件中的echo语句转储文本到标准输出,因此在最终
/dev/null
,但我怎么能包括在无需将整个信息的日志文件(我的意思是信息apt-get
转储,例如Hit http://in.archive.ubuntu.com precise-backports/main Sources
)?
If you can modify the script but not the crontab
then the simplest is to redirect the echoes in the script to stderr
. 如果您可以修改脚本但不能修改
crontab
那么最简单的方法是将脚本中的回显重定向到stderr
。
echo "---------- $(date +"%d %B %G") , $(date +%r) :" >&2
All stdout
will still go to /dev/null
and all stderr
will be logged along with the echoes to stderr
. 所有
stdout
还是会去/dev/null
所有stderr
将与回波登录沿着stderr
。
If you can modify the crontab
but not the script, then the simplest would be devnull 's suggestion. 如果您可以修改
crontab
而不是脚本,那么最简单的方法就是devnull的建议。
If you can modify either, then you have a choice. 如果您可以进行修改,则可以选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.