Home › Forums › OS X Server and Client Discussion › Questions and Answers › automated shutdown
- This topic has 6 replies, 3 voices, and was last updated 15 years, 1 month ago by
tlarkin.
-
AuthorPosts
-
February 3, 2010 at 8:57 pm #377941
blackholemac
ParticipantSounds easy by the topic, but it is not an easy problem. Our district is looking to trim costs off of the utilities budget. I know in advance that I can use MCX Policy in Workgroup Manager to set an automatic shutdown at a given time each day. Sadly, I can only schedule at one time each day using the GUI tools.
We have been handed down for the powers that be that all machines will need to shutdown at 6 p.m. For those users that turn the machines on after the 6 p.m. shutdown, they will receive another one scheduled for 8 p.m. This will happen every two hours until 6 a.m. the next morning.
Unfortunately, Workgroup Manager using the GUI tools can only assign one shutdown time to a single computer group.
I also wish to block the user from canceling automatic shutdowns (we don’t mind if they are warned though and are given a few minutes to save documents and whatnot).
The Windows admins are able to implement this using third-party software. My boss is open to third-party software suggestions, but I figure I might check here first.
My solution ideas right now go in two directions:
1. Create multiple computer groups, each with a different shutdown time and assign a given set of machines to it. (I am going to test this route tomorrow, but do not hold much hope for it considering I would be forcing 6 totally different energy saver to each single machine…likely not going to work according to Apple, but they suggested I try.)
2. Right a cron script/launchd job to that effect. This method seems hopeful except I run into two problems….a.) proper syntax for a scheduled shutdown command that it runs every two hours from 6 p.m. to 6 a.m. the next morning. b.) how to make it display a warning message to the users 10 minutes ahead of the shutdown that they will see in the GUI.
I know the following command works, but assumes the user on a workstation has the terminal open…I want them to get a GUI warning if possible:
shutdown -h +10 “Warning text here”
Hopefully this is specific enough and I can probably answer questions to those who may ask. I thank anyone for help in advance
Brian Martin
Lafayette School Corporation (K-12)
Lafayette, INFebruary 4, 2010 at 3:52 pm #377942blackholemac
ParticipantAlright…I have narrowed this issue significantly with the help of some good ole’ AppleScripting, launchd agents and relying on Workgroup Manager’s Energy Saver MCX setting for the initial 6 p.m. shutdown.
I am posting the AppleScript code I used at the bottom of this message by the way for free use by anyone who sees fit to use it, but I have one single remaining issue in dealing with implementation:
BEGIN APPLESCRIPT CODE
(*
This applet informs the user that a mandatory, forced shutdown will be taking place in five minutes time. The user can either shut down the system immediately or ask for an additional five minutes of time to allow for saving of files. The software was developed by Brian Martin for the Lafayette School Corporation and is free and open for anyone to use as they see fit though the author and Lafayette School Corporation assume ZERO liability for any damage that may be incurred. I advise anyone to compile this as a run-only script so that passwords aren’t easily exposed.
*)set question to display dialog “Include a message to the user explaining the company policy regarding shutdowns and that they should click on one of the buttons to either shutdown immediately or to ask for five minutes of time to save files. It should also explain that no action will default to five minutes and then a forced shutdown.” with title “Automated Shutdown” buttons {“Shutdown NOW!”,”Let Me Have 5 Minutes to Save My Documents”} default button 2 giving up after 300
set answer to button returned of question
if answer is equal to “Let Me Have 5 Minutes to Save My Documents” then
delay 300
do shell script “shutdown -h now” user name “yours here” password “yours here” with administrator privileges
else
do shell script “shutdown -h now” user name “yours here” password “yours here” with administrator privilegesend if
END APPLESCRIPT CODE
The issue: I am having issues formatting a single launchd job that would call this script 7200 seconds during the shutdown hours, but that would not activate the script during normal business hours..
Right now, I am using a very messy method that involves setting up 6 individual launchd jobs that cover each increment from 8 p.m. to 6 a.m. that I want the computer to shutdown.
I hope there is a cleaner way to do this with launchd, but I am lacking the knowledge how to make it so even after re-reading Apple’s Advanced System Administration Certification guide (very detailed and informative resource on launchd) and using Peter Borg’s Lingon.
Hopefully someone here is very skilled at working with launchd and will have some ideas. Please let me know if you need any followup information in assisting me and I thank anyone in advance for their time.
Brian Martin
Lafayette School CorporationFebruary 4, 2010 at 4:25 pm #377943Greg Neagle
ParticipantThe cleanest way to do this is have your cron job or launchd job run every two hours, but have the script itself check to see if the time is between 6pm and 6am. If it’s not, the script exits without doing anything else. If the time is between 6pm and 6am, it can then do the warning and shutdown.
February 5, 2010 at 6:08 pm #377951blackholemac
ParticipantAlright…being that I am a very novice scriptwriter [I come from the old school Mac OS 7,8,9 days 🙂 ] I took the easy way out of this problem. I am using the root user’s crontab instead of launchd to run my AppleScript at various given hours.
I can use the following command and it does properly work and my boss is happy with the functionality:
0 0,2,4,6,18,20,22 * * * /usr/bin/open
Greg, I like your approach the best because it would allow me to use launchd and do this “The Right Way”. I could have a single launchd job run every 7200 seconds and call a wrapper bash script that determines the system time (only in hours and in an integer format) and if it is during business hours, then it would ‘exit 0’ and do nothing, else it would launch the AppleScript and then it would take over from there. I can write a launchd job that runs every 7200 seconds…that’s easy with Lingon but without a proper wrapper script then I’m afraid it would try to force shutdown the computer during business hours.
Sadly, I am not an experienced enough scripter to write such a wrapper script. Nor do I know if it is possible to translate my cron line above to an equivalent launchd plist without writing a wrapper script.
If anyone has any advice on either, I welcome it and thank you for it in advance. If not, I can use cron and all will work fine…I just don’t like relying on technology that may be deprecated.
Brian Martin
Lafayette School CorporationFebruary 6, 2010 at 3:18 am #377954Greg Neagle
ParticipantHere’s a version of my script which does something similar to what you want to do:
[code]
#!/bin/sh# if we’re a laptop, exit. No shutting down laptops (or waking them up unbidden!)
IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep “Model” | grep “Book”`
if [ “$IS_LAPTOP” != “” ]; then
exit 0
fi# check the time; exit if it’s between 5 am and 7 pm
current_hour=`/bin/date +%H`
if [ $current_hour -gt 5 -a $current_hour -lt 19 ]; then
exit 0
fi# now check idle time; exit if we’ve been idle less than 20 minutes
idleTime=`ioreg -c IOHIDSystem | perl -ane ‘if (/Idle/) {$idle=int((pop @F)/1000000000); print $idle,”\n”; last}’`
if [ $idleTime -lt 1200 ]; then
exit 0
fi# tell Power Manager to wake us up or turn us on at 6am M-F
pmset repeat wakeorpoweron MTWRF 06:00:00# check to see if a user’s logged into the console
login_status=`/usr/bin/who | /usr/bin/awk ‘{ print $2 }’`
for i in $login_status; do
if [ $i = “console” ]; then
# someone’s logged in, sleep
osascript -e ‘tell application “System Events” to sleep’
exit 0
fi
done# if we got this far, it’s OK to shut down.
/sbin/shutdown -h now
exit 0
[/code]February 6, 2010 at 2:02 pm #377955blackholemac
ParticipantGreg,
I want to thank you. I felt bad with my original post because it looked like I wanted someone to “do it for me”. That drove my to create my shutdown AppleScript that works really well oddly enough. I knew AppleScript was the easiest language that meets my boss’s requirement that working users be informed graphically of the shutdown. I noticed including a message with the shutdown command does NOT display it graphically so I took my best scripting skills and put together an AppleScript applet that seems to handle shutdown well.
I did try handling the wrapper script myself as well. After your first post, it got me to step away from the computer and understand the scripting logic. I even diagrammed it on paper….shebang, declare a variable that captures the hour, if it is during business hours, exit 0, else if during any other time call the AppleScript with the open command then exit 0.
As for the syntax, I started panicking though when I captured the hour and tried to do math computations with it to test it in a dummy script. I failed miserably. I figured if I couldn’t do math with what was captured, then how would I deal with greater than/less than?
Your script showed me the exact syntax for the greater than/less than. I am grateful.
Brian Martin
Lafayette School CorporationP.S. I am a big fan of your articles on managing MS Office mcx settings and your frustration with Software Update. Your Office advice made it easy for me to force default saving in the old format. Your Software Update thoughts are ones I share. I have wrote a stupid script that calls the command line version of software update but this is a rather clunky solution that Apple could easily make better.
February 16, 2010 at 7:58 pm #377984tlarkin
ParticipantI have a post flight image script that creates user accounts and sets system wide settings on my pristine OS image. I found that using the shutdown command may in fact show an incorrect exit status and it flagged the script to run again (since it’s frequency was set to complete one run after a machine is imaged).
The script looks very sound, but I may recommend you use an Ampersand to put the shutdown process in the background so your script can properly exit. So, for example:
/sbin/shutdown -h NOW &
-
AuthorPosts
- You must be logged in to reply to this topic.
Comments are closed