将带有日期的 stderr 重定向到 Cron 的日志文件 [英] Redirect stderr with date to log file from Cron
本文介绍了将带有日期的 stderr 重定向到 Cron 的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
bash 脚本从 cron 运行,stderr 重定向到日志文件,这一切正常.代码是:
A bash script is run from cron, stderr is redirected to a logfile, this all works fine. The code is:
*/10 5-22 * * * /opt/scripts/sql_fetch 2>> /opt/scripts/logfile.txt
我想在日志文件的每一行前面加上日期,这不起作用,代码是:
I want to prepend the date to every line in the log file, this does not work, the code is:
*/10 5-22 * * * /opt/scripts/sql_fetch 2>> ( /opt/scripts/predate.sh >> /opt/scripts/logfile.txt )
predate.sh 脚本如下所示:
#!/bin/bash
while read line ; do
echo "$(date): ${line}"
done
所以第二位代码不起作用,有人可以解释一下吗?谢谢.
So the second bit of code doesn't work, could someone shed some light? Thanks.
推荐答案
我有一个小脚本 cronlog.sh 来执行此操作.脚本代码
I have a small script cronlog.sh to do this. The script code
#!/bin/sh
echo "[`date`] Start executing $1"
$@ 2>&1 | sed -e "s/(.*)/[`date`] 1/"
echo "[`date`] End executing $1"
那你就可以了
cronlog.sh /opt/scripts/sql_fetch >> your_log_file
示例结果
cronlog.sh echo 'hello world!'
[Mon Aug 22 04:46:03 CDT 2011] Start executing echo
[Mon Aug 22 04:46:03 CDT 2011] helloworld!
[Mon Aug 22 04:46:03 CDT 2011] End executing echo
这篇关于将带有日期的 stderr 重定向到 Cron 的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文