Replace sterr message by string

2019-09-14 19:57发布

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

So when there's an error I don't know how to replace the error output by the string "$(date) Finish with errors"

1条回答
地球回转人心会变
2楼-- · 2019-09-14 20:29

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 

Append stdout to file and stderr to NULL as >> /path/to/logFile.log 2>/dev/null

查看更多
登录 后发表回答