I wanted to take a moment to post our changes to a script included in the resource kit that has been very useful to us.
The script in question is the timedForcedShutdown.sh script.
My changes remove the extraneous postpone dialog box and adds a $postponeButtonText variable to allow customization of the postpone button.
Now, when the postpone button is pushed by the user, the script does an exit 0 instead of displaying the second dialog box included in version 1.0. That second dialog box caused Terminal to blow an error during my testing of the original 1.0 version. As such, the $postponeAlert variable was removed.
The new version also implements a "safety mechanism" that prevents the script from executing during business hours and scaring folks. You can define the $openbusinessHour
$closebusinessHour variables to set those times.
Finally Version 1.2 builds on the 1.0 version author's assumed desire to offer the sysadmin easy and full customization of the GUI presented to the user. I hope folks find use for this and if the 1.0 author is reading this, thank you for your hard work and hope my changes have been a worthy addition (or subtraction).
#!/bin/sh
####################################################################################################
#
# Copyright (c) 2010, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
#
# SUPPORT FOR THIS PROGRAM
#
# This program is distributed "as is" by JAMF Software, LLC's Resource Kit team. For more
# information or support for the Resource Kit, please utilize the following resources:
#
# http://list.jamfsoftware.com/mailman/listinfo/resourcekit
#
# http://www.jamfsoftware.com/support/resource-kit
#
# Please reference our SLA for information regarding support of this application:
#
# http://www.jamfsoftware.com/support/resource-kit-sla
#
####################################################################################################
#
# ABOUT THIS PROGRAM
#
# NAME
# timedForcedShutdown.sh -- This script is used to enforce a mandatory shut down.
#
# SYNOPSIS
# sudo timedForcedShutdown.sh
# sudo timedForcedShutdown.sh <mountPoint> <computerName> <currentUsername> <minutesN>
# <shutdownAction> <notificationMessage> <shutdownPhrase> <shutdownTitle>
# <postponeButtonText> <openbusinessHour> <closebusinessHour>
#
# DESCRIPTION
# This script will help to enforce a mandatory reboot or shut down.
#
# If no console user is logged in, the script will execute the command
# stored in the $shutdownAction variable.
#
# If a console user is logged in, a dialog is displayed informing the user
# of the number of minutes until shutdown followed by a configurable
# message stored in $notificationMessage. The dialog contains two buttons.
#
# Clicking "Postpone for 2 Hours" will delay the shutdown for two hours when used
# in combination with cron, launchd or a valid policy on the JSS.
#
# Clicking "Shut Down" will execute the command stored in the $shutdownAction variable.
#
##########################################################################################
#
# HISTORY Version: 1.2
# - Version 1.0 created by Miles A. Leacy IV on May 20th, 2009
# - Version 1.2 modifications by Brian Martin on September 25, 2012
#
# Version 1.2 removes the extraneous postpone dialog box and adds a $postponeButtonText
# variable to allow customization of the postpone button.
#
# When the postpone button is pushed by the user, the postpone button now does an exit 0
# instead of displaying the second dialog box in version 1.0. This second dialog box
# caused Terminal to blow an error when executing the script. As such, the $postponeAlert
# variable included in version 1.0 was removed.
#
# Version 1.2 also implements a "safety mechanism" that prevents the script from executing
# during business hours and scaring folks. You can define the $openbusinessHour
# $closebusinessHour variables to set those times.
#
# Finally Version 1.2 builds on Miles Leacy's assumed desire to offer the sysadmin easy
# and full customization of the GUI presented to the user.
#
##########################################################################################
#
# DEFINE VARIABLES AND READ-IN PARAMETERS
#
##########################################################################################
minutesN=5
# Number of minutes to count down before shutdown
shutdownAction="shutdown -h now"
# Change to "shutdown -r now" to reboot
# Change to "shutdown -h now" to shut down
notificationMessage="Please save all open documents and close any open applications prior to a shut down."
# This message will appear in the initial dialog box following the initial warning dialog.
shutdownPhrase="Shut Down"
# This variable should contain either "Shut Down" or "Restart"
# depending on the value of $shutdownAction. This string will appear
# in the dialog and will determine the name of the button that causes
# $shutdownAction to be executed.
shutdownTitle="LSC Automated Shutdown"
# You can customize this to your organization as you see fit.
postponeButtonText="Postpone for 2 Hours"
# Basically this button could say anything as it causes the script to exit 0 when pushed.
openbusinessHour="6"
closebusinessHour="14"
# You would use military time by hour here, for example 6 = 6 a.m., 18 = 6 p.m.
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "minutesN"
if [ "$4" != "" ] && [ "$minutesN" == "" ]; then
minutesN=$4
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 5 AND, IF SO, ASSIGN TO "shutdownAction"
if [ "$5" != "" ] && [ "$shutdownAction" == "" ]; then
shutdownAction=$5
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 6 AND, IF SO, ASSIGN TO "notificationMessage"
if [ "$6" != "" ] && [ "$notificationMessage" == "" ]; then
notificationMessage=$6
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 7 AND, IF SO, ASSIGN TO "shutdownPhrase"
if [ "$7" != "" ] && [ "$shutdownPhrase" == "" ]; then
shutdownPhrase=$7
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 8 AND, IF SO, ASSIGN TO "shutdownTitle"
if [ "$8" != "" ] && [ "$shutdownTitle" == "" ]; then
shutdownTitle=$8
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 9 AND, IF SO, ASSIGN TO "postponeButtonText"
if [ "$9" != "" ] && [ "$postponeButtonText" == "" ]; then
postponeButtonText=$9
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 10 AND, IF SO, ASSIGN TO "openbusinessHour"
if [ "$10" != "" ] && [ "$openbusinessHour" == "" ]; then
openbusinessHour=$10
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 11 AND, IF SO, ASSIGN TO "closebusinessHour"
if [ "$11" != "" ] && [ "$closebusinessHour" == "" ]; then
shutdownTitle=$11
fi
##########################################################################################
#
# SCRIPT CONTENTS
#
##########################################################################################
# exit 0 if the times is during declared business hours
current_hour=`/bin/date +%H`
if [ $current_hour -gt $openbusinessHour -a $current_hour -lt $closebusinessHour ]; then
exit 0
fi
# If no user is logged in at the console, shut down immediately
consoleUser=`/usr/bin/w | grep console | awk '{print $1}'`
if test "$consoleUser" == ""; then
$shutdownAction
fi
function timedShutdown {
button=`/usr/bin/osascript << EOT
tell application "System Events"
activate
set shutdowndate to (current date) + "$minutesN" * minutes
repeat
set todaydate to current date
set todayday to day of todaydate
set todaytime to time of todaydate
set todayyear to year of todaydate
set shutdownday to day of shutdowndate
set shutdownTime to time of shutdowndate
set shutdownyear to year of shutdowndate
set yearsleft to shutdownyear - todayyear
set daysleft to shutdownday - todayday
set timeleft to shutdownTime - todaytime
set totaltimeleft to timeleft + {86400 * daysleft}
set totaltotaltimeleft to totaltimeleft + {yearsleft * 31536000}
set unroundedminutesleft to totaltotaltimeleft / 60
set totalminutesleft to {round unroundedminutesleft}
if totalminutesleft is less than 2 then
set timeUnit to "minute"
else
set timeUnit to "minutes"
end if
if totaltotaltimeleft is less than or equal to 0 then
exit repeat
else
display dialog "ATTENTION!!!! ATTENTION!!!! ATTENTION!!!!
In an effort to conserve energy during evenings and weekends, this Mac is scheduled to " & "$shutdownPhrase" & " in " & totalminutesleft & " " & timeUnit & ".
" & "$notificationMessage" & " " giving up after 60 buttons {"$postponeButtonText", "$shutdownPhrase"} default button "$shutdownPhrase" with title "$shutdownTitle"
set choice to button returned of result
if choice is not "" then
exit repeat
end if
end if
end repeat
end tell
return choice
EOT`
if test "$button" == "$postponeButtonText"; then
`exit 0`
else
$shutdownAction
exit 0
fi
}
timedShutdown