Hello All.
I am not what I would consider a good shell scripter. I do aspire to improve though and thought peer review might point out flaws and/or better/easier ways of doing things. I think a severe issue with my capabilities is that I have never really started from the beginning, building upon the basics. I usually just look at another script, decipher what is happening and, modify the syntax and relevant parts for what I need. Much of this is often a blink guess as I have no idea why some things are happening - I just follow the leader.
I've been cleaning up some scripts and thought it would be nice to hear from wiser people what might be improved upon. It might even help out others like me. So, with that in mind, I have a script which I am using to manage uptime on machines. It restarts machines that have no console user after 13 days and if machines have a logged in user, alerts users starting with e-mail and then moving to screen alerts. I have it running once a day on all machines using the every 15 trigger.
Original credit for the script goes to Steve Wood. It's been changed a bit to suit our needs. I would love to hear how it might be improved in functionality or design. Any takers?
#!/bin/bash
## Originally based off of a script by Steve Wood
## Modified a "a bit" by Aaron Robinson
## 2011/05/13
## SET DEFAULT VALUES - Default values will be overwritten via $4 and $5 passed
# Set the number of days at which you wish to alert users
# Alert via e-mail
emailwarn=6
# Alert via screen popup
popupwarn=12
# Restart days if no user logged in
restardays=13
# USE VALUES FROM $4 and $5 IF PASSED
# Use $4 for e-mail warning
if [ "$4" != "" ]; then
emailwarn=$4
fi
# # Use $5 for popup warning
if [ "$5" != "" ]; then
popupwarn=$5
fi
# set some variables and grab the uptime
days=uptime | awk '{ print $4 }' | sed 's/,//g'
num=uptime | awk '{ print $3 }'
consoleuser=ls -l /dev/console | awk '{ print $3 }'
# Check to see if it's been up for short time
if [[ $days != "days" ]]; then
logger -i "$0: Machine is up less than 2 days. No business here. Exiting."
exit 0
# Just restart if nobody is logged in and the machine has been up for 7+ days.
elif [[ $days = "days" && $num -gt $restardays && $consoleuser = "root" ]]; then
logger -i "$0: No user is logged in and machine is up for $num days. Restarting machine."
/sbin/reboot
exit 0
# If someone is logged in
elif [[ $days = "days" && $consoleuser != "root" ]]; then
# If we are here, then there's someone logged in and it's been more than a couple of days. Now check how long they've been logged in and alert accordingly.
# Now send the correct notices depending on days up if [ $num -gt $emailwarn ]; then
# we want the computer name to ID the computer in the mail message
computername=systemsetup -getComputerName
# set the message message1="You have been logged into $computername which has not been restarted for $num days. It is" message2="important to restart machines regularly to order to run efficiently and to apply updates " message3="that have been pushed in the background. Please restart your machine ASAP. Thank you."
# grab the current user and query AD for the email address
CurrentUser=ls -l /dev/console | awk '{ print $3 }'
email=dscl "/Active Directory/All Domains/" -read /Users/$CurrentUser | grep EMailAddress | awk '{ print $2 }'
touch /Library/Application Support/JAMF/Receipts/UpTooLong.pkg
echo "$message1
$message2
$message3" | mail -s "Machine Up Too Long" $email
if [ $num -gt $popupwarn ]; then
# Screen Popup to logged in user
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType hud -title "PLEASE RESTART" -description "Your computer has been up for $num days. Please restart ASAP"
fi
fi
fi
# now run a recon
# /usr/sbin/jamf recon
exit 0
