Best method to schedule a reboot for a specific time

steventhemacman
New Contributor III

Hello

I am being asked to schedule a reboot for our student machines at a specific time, say 11:30 - 11:45 am. I know I can set a policy to start and end at a specific time, but not sure how to actually get the laptops to reboot during a specific time is different, as I'm not sure the best trigger, etc. Just looking for ideas. Thanks!

12 REPLIES 12

pblake
Contributor III

You can use the pmset command to edit the energy saver settings to set a startup, shutdown, or reboot. I would use that. The policy can list the command with the date you want to restart and the can run the policy now and wait.

Rayfield
New Contributor III

I have one set to run in the middle of the night at check-in and once per day, though one thing to keep in mind is if a user is logged in and you select "Restart" instead of "Restart Immediately" someone will need to hit the "OK" button in order for it to restart, kind of annoying especially if someone leaves the computer logged in over night. If you select "Restart Immediately" it won't allow the user to save their work.

I also have it set to not run during the work day, you could set it in theory to not run between 11:45am-11:30am (If your check-in time is scheduled for 15 minutes it should get them all)

Though, we were having an issue with it where it seems to restart during the middle of the school day even though it's well without the scheduled reboot. We've turned it off recently due to Standardized Testing, but my guess is either the computers aren't logging out at the end of the night, hit the OK, and then it reboots 10 minutes later, causing the end users to think their computers are rebooting at random. Either that, or a weird time syncing issue where the time isn't correct on the random desktops that reboot during the day.

But as for the actual policy, just select the "Restart Options" category and select your options in there.

mm2270
Legendary Contributor III

Does pmset allow for a scheduled reboot? I don't think it does unless its an undocumented setting. Looking at the man page for pmset, under the Scheduled Event Argument section, it lists:

type - one of sleep, wake, poweron, shutdown, wakeorpoweron

No mention of reboot or restart there. @pblake If you know of a way to also have it restart instead of powering off, I'd be curious to see how you're doing it with pmset.

steventhemacman
New Contributor III

I kind of wish there was a way to not have it ask for the OK. But just tell them it's going to restart in 5 minutes. It seems you can do restart immediately, or they have to click OK. I'm sure the students who know were trying to lock something down (we are and it requires a reboot while at school) just won't click OK.

stevewood
Honored Contributor II
Honored Contributor II

@steventhemacman since pmset doesn't provide the capability to schedule a restart, and you would like the restart to occur even if the user doesn't click OK, I might tackle this with a couple of LaunchDaemons and a script.

So, first LaunchDaemon would be set to trigger on a set day at a set time. For example, the following would trigger at 11:45 am on Tuesday. You could set this to a specific date instead if you wanted to (launchd man page.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.company.sysreboot</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/script</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>11</integer>
            <key>Minute</key>
            <integer>45</integer>
            <key>Weekday</key>
            <integer>2</integer>
        </dict>
    </array>
</dict>
</plist>

The script would do two things. First, it would use jamfHelper or cocoaDialog or yo to alert the user that the restart was going to occur in 5 minutes. Second, at the same time as the alert dialog goes up, and second LaunchDaemon is created and loaded that begins the 5 minute countdown. This would insure that the machine rebooted 5 minutes from alerting the end user.

#!/bin/sh

# name: sysreboot.sh
# purpose: alert the user and reboot the machine in 5 minutes

CD="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog"

cdTitle="SYSTEM RESTART"

cdText="YOUR SYSTEM WILL RESTART IN 5 MINUTES. PLEASE SAVE ALL OPEN WORK AND CLOSE ANY OPEN APPS."

cdIcon="/System/Library/CoreServices/ReportPanic.app/Contents/Resources/ProblemReporter.icns"

bubble=`$CD bubble --title "$cdTitle" --no-timeout --text "$cdText" --icon-file $cdIcon`

# now create the launchdaemon and load it

# create the plist file:
echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.company.rebootnow</string>
<key>ProgramArguments</key>
<array>
    <string>shutdown -r 5</string>
</array>
<key>RunAtLoad</key>
<true/>
/dict>
</plist>' > /Library/LaunchDaemons/com.rebootnow.plist

# change ownership
chown root:wheel "/Library/LaunchDaemons/com.rebootnow.plist"
chmod 644 "/Library/LaunchDaemons/com.rebootnow.plist"

launchctl load /Library/LaunchDaemons/com.rebootnow.plist

# remove the LaunchDaemons so that they do not kick off after the restart
srm /Library/LaunchDaemons/com.company.sysreboot.plist
srm /Library/LaunchDaemons/com.rebootnow.plist

The above uses cocoaDialog, but as I stated, you could use any of the three methods I mentioned. The advantage of using LaunchDaemons is that you could load everyone's systems on Friday and not have it kick off until Tuesday at 11:45 am.

The policy you would use to deploy this would just need to make sure to load the first LaunchDaemon as part of the policy. So in the Files & Processes tab of the policy, under Execute Command, you would want to have the following:

launchctl load /Library/LaunchDaemons/com.company.sysreboot.plist

Of course, you'd need to package all of this up to deploy to your machines (make sure to set proper ownership and permissions on the com.company.sysreboot.plist file).

I just threw this together without testing, so test, test, test before deploying.

Anyone see anything wrong with this method?

HTH

mm2270
Legendary Contributor III

You could just put this in the Run Command field, or plop it into a script:

/sbin/shutdown -r +10; /usr/sbin/jamf displayMessage -message "Your Mac will reboot in 10 minutes. Please begin to close any open work now to prevent any data loss."

No need to wait for the OK button to be clicked with the above. Change the 10 to any other number you want, and have it run based on a timeframe or a trigger, etc.

That said, I have a much more advanced reboot scheduler script that I'm developing with another Mac admin. Its not released anywhere yet, but it will have the ability to either take a parameter from Casper when run to schedule a reboot to a future date/time and alert the user what it was scheduled to, or, ask for input from the user, giving them several reboot deferral options to choose from. The deferral timeframes can be easily customized. Once the reboot is scheduled, it does not rely on Casper since it will use a LaunchDaemon. It will remind users of the impending reboot at specified time intervals (again that can be customized), and give them a final 5 minute countdown before it reboots.

Its similar to what @stevewood posted above, but works in a somewhat different way. Same general idea though.

pblake
Contributor III

I was wrong. It happens a lot. pmset only schedules wakes and shutdowns. I like @stevewood's suggestion. It looks pretty solid.

steventhemacman
New Contributor III

Thanks @stevewood and @mm2270 and @pblake. I will do some testing and I think I will be successful with the help given here, I appreciate it. I like to have different approaches to the issue. As for pmset I could always just do a shutdown, I would hope the kids can figure out how to turn in back on ; )

Thank again everybody!

Look
Valued Contributor III

I am using pmset, not so much as a weekly schedule, more a wake up at a specific time if the machine falls into a smart group with certain criteria. Script I use is a quasi random wake in the early hours oft he following morning.

#!/bin/bash
TomorrowsDate=`date -v +1d +"%m/%d/%Y"`
RandomMinutes=`date +"%S"`
pmset schedule wakeorpoweron "$TomorrowsDate 01:$RandomMinutes:00"

There are a number of ways pmset can do ti though, including schedules.

2bits
New Contributor

Hi, the man page for pmset does not document a scheduled restart - it only quotes (for type) sleep, wake, poweron etc. However you can do (eg) 'pmset repeat restart MTWRFSU 17:00:00' and it appears to work. If you go to Energy Saver in System Preferences, you'll see your input in the GUI (via Schedule). Changes via the GUI (Energy Saver) or 'pmset repeat' - affect /Library/Preferences/SystemConfiguration/com.apple.AutoWake.plist, in the same way. Note that actual restart happens 10 mins after you have scheduled. (That's what you'd expect had you implemented the change via Sys Prefs / Energy Saver). Doing it via 'pmset repeat restart' seems to amount to the same thing. BTW I'm using Yosemite. My guess is, that no-one has gotten around to updating pmset man page.

sayensu
New Contributor

Hi, Is there a way to script if a Mac does not have a student logged on to restart rather than having a fixed schedule to reboot? For example, we have policies that get activated once the Mac restarts but if students are constantly on them, it's impossible for the policy to be applied.

Look
Valued Contributor III

@sayensu Use one policy with a script with pmset to schedule a wake for the machine when you want the policy to run.
Have a another policy with a restricted activation time that checks for a console user and restrarts if there is none, just make sure the scheduled wake falls within the activation time and you will be good, any machines turned on will wake up, check for a user and restart if none are present.
The second policy can probably be managed simply with Caspers builtin restart tools, the first will need a tiny script similar to the one I posted earlier but with just wake rather than wakeorpoweron, otherwise powered off machines will also power on and then imeddiately restart.

Personally we restart them in the middle of the night regardless of whether a student is logged in...