I have a bash script file run_job.sh
and another script file send_email.sh
.
I want all run_job.sh's output to go to a log file and only errors (if any) to be sent to the file send_email.sh.
send_email.sh is a file that expects two args- email body and email subject. The error should be sent as the email body.
Here's my code:
run_job.sh
{
printf "\n----- PROCESS STARTED %s -----\n" "$(date +%Y-%m-%d\ %H:%M:%S)"
COMMAND="cd $JOB_FOLDER_PATH && $EXEC_COMMAND"
printf "Executing command: %s\n\n" "$COMMAND"
eval "$COMMAND"
COMMAND_EXIT_CODE=$?
printf "\n----- PROCESS ENDED %s -----\n\n" "$(date +%Y-%m-%d\ %H:%M:%S)"
} &>> "$OUT_FILE" 2> ERROR_OUTPUT # this doesn't work
if [ $COMMAND_EXIT_CODE -ne 0 ]; then
bash ./send_email.sh "Job Failed" $ERROR_OUTPUT
fi
send_email.sh
MAIL_SUBJECT=$1
MAIL_BODY=$2
echo "$MAIL_BODY" | \
mail -a "From: Kaizen Service <service@kaizen.co.uk>" \
-a "Subject: $MAIL_SUBJECT" \
"$(grep -o '^[^#]*' $MAILING_LIST_FILE | tr '\n' ',')"