用字符串替换 sterr 消息 [英] Replace sterr message by string
问题描述
我正在寻找一种记录 crontable 事件的方法.因此通过在日志文件中写入执行日期和结果.例如
I'm looking for a way to log crontable events. thus by writing in a log file the date of execution and the result. for example
Tue Feb 14 11:17:01 CET 2017 Finish with success
Tue Feb 14 11:18:01 CET 2017 Finish with errors
所以对于第一个,我找到了一种方法.通过这种方式:
So for the first one i've found a way to do it. By this way:
0,5,10,15 * * * * /path/to/script $args 2>> /path/to/logFile.log && echo "$(date) Finish with success" >> /path/to/logFile.log
所以当出现错误时我不知道如何用字符串$(date) Finish with errors"替换错误输出
So when there's an error I don't know how to replace the error output by the string "$(date) Finish with errors"
推荐答案
使用布尔运算符 ||
添加登录失败条件,在命令失败时执行,
Add the log on failure condition with the Boolean ||
operator which executes on failure of the command,
0,5,10,15 * * * * /path/to/script "$args" >> /path/to/logFile.log 2>/dev/null && echo "$(date) Finish with success" >> /path/to/logFile.log \
|| echo "$(date) Finish with errors" >> /path/to/logFile.log
将 stdout
附加到文件并将 stderr
附加到 NULL
作为 >>/path/to/logFile.log 2>/dev/null
Append stdout
to file and stderr
to NULL
as >> /path/to/logFile.log 2>/dev/null
这篇关于用字符串替换 sterr 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!