将dateredirectstderr以从Cronlogging文件

一个bash脚本从cron运行,stderr被redirect到一个日志文件,这一切工作正常。 代码是:

*/10 5-22 * * * /opt/scripts/sql_fetch 2>> /opt/scripts/logfile.txt 

我想在日志文件的每一行添加date,这是行不通的, 代码是:

 */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 

所以第二个代码不起作用,有人可以点亮一下吗? 谢谢。

Solutions Collecting From Web of "将dateredirectstderr以从Cronlogging文件"

我有一个小脚本cronlog.sh来做到这一点。 脚本代码

 #!/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 
 */10 5-22 * * * (/opt/scripts/predate.sh; /opt/scripts/sql_fetch 2>&1) >> /opt/scripts/logfile.txt 

应该是你的方式。