简体   繁体   中英

run yum command and email results… all in a cron bash shell job

I am cobbling my first Linux shell command. This command runs a yum command and email results in a periodic cron bash shell job. I am OK up to the email part where I get a "No such file or dir" error on email address(!). Can someone unravel syntax and provide method that works. Can be other shell scripting language if bash is not best for this. Seem to be having trouble with multiple line commands.

#!/bin/bash

body="Some Text"

## output yum command to a work file
echo $body > /home/security_check.txt
yum --security check-update >> /home/security_check.txt

## this works!
## mail -s 'Linux Security patches required' bob@example.com < /home/security_check.txt

## this does not 
mail \
-a "From: root@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "Linux Security patches required" \
bob@example.com \
< /home/security_check.txt


## error message:
## From: root@example.com: No such file or directory

Take a look at this post on sending HTML email with Unix mail . https://unix.stackexchange.com/questions/15405/how-do-i-send-html-email-using-linux-mail-command

Seems that your need for the -a flag is to send HTML mail.

Here is solution that worked for me. Requires the mailx, yum and yum-plugin-security modules. It seems that mailx behaves differently on different distributions. Comments welcome

#!/bin/bash

## script name: yum_security_patch_report.sh
## description: emails list of security patches, saved in /etc/cron.monthly so it will be sent monthly

##  generate yum security report
yum --security check-update > /home/security_check.txt

## get server hostname
HOSTNAME=echo hostname

## compose subject
SUBJECT="List of Linux security patches required - for server $HOSTNAME"

## set up TO and CC
TO="bob@example.com"
CC="sue@example.com"

## get report summary from file saved, uses $(code) notation to catch as variable
SUMMARY=$(grep 'needed for security'   /home/yum_security_check.txt)

RUNFROM=($"$0")

## compose body of email
MAILBODY="Find enclosed report of Linux modules that require security patches.

$SUMMARY

This report comes from server: $HOSTNAME. This script is being run from: $RUNFROM"

## send email with mailx command
echo "$MAILBODY" | mailx -v -s "$SUBJECT" -c "$CC" -a /home/security_check.txt  "$TO"

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