Sample Header Ad - 728x90

How to send email with log output if script fails

0 votes
1 answer
864 views
I have the following script template I've been writing to use LFTP to mirror remote files to a local folder for clients. If the mirror fails, I'd like it to send an email with the output that was logged (but not the actual log file, as it could be rather long). As is, it would only show the exit status of 1. What's the best way to send the output instead? #! /bin/bash # Client info client=example data=/home/clients/$client/data log=/home/clients/$client/log # Create directories mkdir -p $data $log # LFTP settings protocol="sftp://" host="ftp.example.com" user="example" pass='123abc' remote=/Outbound command="mirror --verbose --continue $remote $data" # Output to log exec 3>&1 4>&2 trap 'exec 2>&4 1>&3' 0 1 2 3 exec 1>>$log/$client.log 2>&1 # Begin import | Recursively mirror remote to data timestamp="$(date +"%m-%d-%Y@%T")" echo "*** Time is $timestamp ***" echo "Starting $client import." lftp -u $user,$pass $protocol$host <
Asked by Justin Buckley (1 rep)
Jan 29, 2021, 05:02 PM
Last activity: Jan 7, 2025, 12:35 PM