简体   繁体   中英

bash send errors by email

I wrote some bash script that does some backup job. I run the script with errexit and pipefail so I won't miss any error. The thing I want now is the script sending me an email in case an error occures. I got this working. But I want the scripts errors to be included in the email body. How can I do this?

here is the script:

#!/bin/bash
# exit script if an error occures
set -o errexit
# even exit if an error in passed through a pipe
set -o pipefail

trap 'ERRORMESSAGE_HERE | mailx -s "Something went wrong on srv-002" mymail@mycompany.ch' ERR

# the backup job here

# if script reaches this point everything went perfectly good
echo "all good!"

Thanks a lot for your help!

how about(untested):

{
    # do the work here
} 2> >(mailx -s "job errors" recipient@example.com)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM