Replace sterr message by string

2019-09-14 20:31发布

问题:

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:

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