1

I have a script that syncronizes folders. The script does

/usr/bin/rsync -av --delete $SOURCE $DEST >> $LOG_FILE 2>> $ERR_LOG_FILE 

so it redirects output to LOG_FILE and error output to ERR_LOG_FILE.

In addition, I would like the errors sent to root via e-Mail. How can I do that?

    2 Answers 2

    1

    If you can rely on the exit code....

    /usr/bin/rsync -av --delete "$SOURCE" "$DEST" >> "$LOG_FILE" 2>&1 || mail -s "Rsync errors" [email protected] <"$LOG_FILE" 
    3
    • The email will include old error messages from before this run of the script.
      – Barmar
      CommentedDec 10, 2024 at 21:14
    • Yup, OPs logic, not my invention.
      – symcbean
      CommentedDec 11, 2024 at 9:14
    • But isn't that part of what they need to resolve?
      – Barmar
      CommentedDec 11, 2024 at 17:02
    0

    With bash:

    rsync -av --delete "$SOURCE" "$DEST" >> "$LOG_FILE" 2>>"$ERR_LOG_FILE" [[ -s $ERR_LOG_FILE ]] && mail -s test root < "$ERR_LOG_FILE" 
    4
    • it works, but: 1) the email is sent even if there are no errors, 2) the redirection to file delete all previous logs. I'm new in scripting and for the second point I think depends from the redirection symbol (>); should I put a second > before the first bracket ? ... 2> >>(tee ..... ?
      – klatls
      CommentedDec 10, 2024 at 15:40
    • @Roberto does the $ERR_LOG_FILE already exist? I assume it does since you are appending to it with >> and not overwriting. Is that essential? Gilles's solution depends on the logfile being empty if the current invocation of rsync had no errors. Of course, that will never be the case if using rsync -v since that always writes to stder, but even if you remove that, any previous errors would have to be cleared out first.
      – terdon
      CommentedDec 10, 2024 at 16:01
    • @terdon rsync -v is for the $LOG_FILE
      – klatls
      CommentedDec 10, 2024 at 16:31
    • @Roberto OK, but in that case the log file will never be empty, right? So this will always send an email as you said in your first comment.
      – terdon
      CommentedDec 10, 2024 at 17:10

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.