I'm looking to cleanup the following shell script that reads in the uptime of a Linux server and will mail a certain user when the threshold has been met so that we know when to reboot through a daily cron job. The below will read in the uptime and output the result to a file, which will then be emailed. I'd also like to add in the hostname of the server either the report or the email heading. Fairly new to shell scripting so any/all tips are appreciated. Thanks!
#!/bin/bash
timeup () { uptime | awk '{print $3}'; }
UPTIME=100
if [ $(timeup) -ge $UPTIME ]; then
#output uptime to report
uptime | awk '{print $3,$4}' | sed 's/,//' >> /opt/scripts/report.out
#mail report
cat /opt/scripts/report.out | mail -s "Server Needs To Be Rebooted" "your@emailaddress.com"
#remove old file
rm -f /opt/scripts/report.out
fi
timeup () { uptime | awk '{print $3}'; }
timeup () { uptime | awk '{print $3}'; }
result will be something like that: " 45:45, ". There are 2 problems with this:
This is better (but not so nice:) ): timeup () { uptime | awk '{print $3}' | awk -F\: '{print $1}';}
timeup () { uptime | awk '{print $3}' | awk -F\: '{print $1}';}
Second awk command divide hour and minute, and print only hours.
I prefer sendEmail script instead of mail command: http://manpages.ubuntu.com/manpages/xenial/man1/sendEmail.1.html
I'd put hostname in the subject: "Server Needs To Be Rebooted: $(hostname -f)"
hostname -f
command print out FQDN
You can merge uptime... and mail command lines and remove rm command:
uptime | awk '{print $3,$4}' | sed 's/,//' | mail -s "Server Needs To Be Rebooted" "your@emailaddress.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.