重定向标准错误与日期从cron日志文件 [英] Redirect stderr with date to log file from Cron

查看:232
本文介绍了重定向标准错误与日期从cron日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个bash脚本由cron运行,标准错误将被重定向到一个日志文件,这一切工作正常。
的code是:

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

我要prePEND日在日志文件中的每一行,这也不行,的code是:

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

所以,code的第二位不起作用,可能有人提供一些线索?
谢谢你。

So the second bit of code doesn't work, could someone shed some light? Thanks.

推荐答案

我有一个小脚本cronlog.sh做到这一点。该脚本code

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

这篇关于重定向标准错误与日期从cron日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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