Scheduling a Shut Down on macOS
It is possible to schedule a Mac to shut down at a particular time or after a specific event. There are different approaches available depending on the situation.
In this recipe we will look at the options available and their caveats.
Built in to macOS
macOS has built in support for scheduling shut downs based on the time and day of the week. Before macOS 13, this functionality was available in the System Preferences’ Energy Saver settings.
With macOS 13, Apple removed the user interface and relegated the functionality to the
pmset
Terminal command. The
macOS User Guide explains how to schedule a power event using the command line:
- In the Terminal app on your Mac, enter a
pmset
command. See thepmset
man page for the list of commands you can use. For example, try any of the following:
pmset -g sched
: See the current schedule.sudo pmset repeat wake M 8:00:00
: Schedule your Mac to wake at 8:00 am every Monday.sudo pmset repeat cancel
: Cancel the current schedule.- Press Return.
Schedule a Shut Down with pmset
Using the built in
pmset
approach, a scheduled shut down is created with the command:
sudo pmset repeat shutdown MW 18:00:00
This command schedules macOS to shut down at 6pm every Monday and Wednesday.
sudo
is needed because a scheduled shut down affects all users of the computer.
Cancelling the pmset
Shut Down
To cancel this pmset
scheduled shut down, use the following command:
sudo pmset repeat cancel
Caveats of Scheduling with pmset
There are caveats to using the pmset
scheduler and these are listed in the User Guide:
Your Mac must be awake and you must be logged in for it to shut down at the scheduled time. If you’re not logged in or if your Mac is in sleep, it won’t shut down.
…
Note: If you have any documents open with unsaved changes, your Mac might not go to sleep or shut down when scheduled.
These caveats limit the usefulness of the built in pmset
approach:
- someone must be logged into the Mac;
- shut down can be interrupted.
The pmset
manual adds another caveat:
Note that you may only have one pair of repeating events scheduled - a “power on” event and a “power off” event.
If you need multiple scheduled shut downs at differing times or days, or want to be certain the Mac will shut down, then you need more than the built in offering.
Safer Shut Down
Over ten years ago I wrote about how Power Manager shuts down macOS in Shutting Down Your Mac Safely. Power Manager’s approach differs from macOS’s by focusing on ensuring the shut down occurs.
When a shut down is scheduled in Power Manager, you can be confident the shut down will occur. This comes with its own caveats and these are important to understand. The caveats are:
- if open applications contain unsaved work, that work may be lost;
- applications will be quit and will not auto-restore their state when logging back in.
Well written modern applications will automatically store and restore unsaved work. Unfortunately, not all applications are modern or well written.
No Surprises
Before shutting down the computer, Power Manager will notify active users what is about to happen and provide time to save their work. This notification and subsequent warning as the shut down begins is deliberate. The notification tells the user what is about to happen and hopefully helps them avoid the loss of unsaved changes.
The notification also stops users being surprised when the computer shuts down without their involvement. Note that the macOS built in scheduled shut down does not provide any notification.
Administrators can delay or cancel pending shut downs scheduled by Power Manager; this can be performed locally or remotely.
Robust at a Cost
The second caveat about applications not auto-relaunching and restoring their windows, stems from how Power Manager achieves a safer shut down.
Before shutting down, Power Manager individually quits each application. This provides the opportunity to deal with applications that might block shut down but unfortunately it also means application and window restoration is affected.
This is something we want to improve. Today Power Manager errs on ensuring the schedule is reliably performed.
Schedule a Shut Down with Power Manager
Let’s use
Power Manager to schedule a shut down to match the Monday and Wednesday at 6pm pmset
example above.
- Launch Power Manager.app
- Add a new event; select the menu item: File > New > New Event…
- Select the Schedule Assistant task Power off daily.
- Within the What step, select Shut Down from the pop-up menu.
- Continue to the When step. Enter the time to shut down and select the desired days of the week.
- Continue to the Why step. Name your event and add any notes you desire. The name will be seen by the user of the computer.
- Continue to the Confirm step.
- Add to confirm the creation of the new event.
The shut down event has been created and is active.
Cancel a Shut Down with Power Manager
The event created above can be cancelled or delayed from the Power Manager menu bar item or Scheduler View:
- Power Manager.app > Scheduler (menu) > Show Scheduler
Altering the pending shut down event requires administrator privileges. This is for the same reason pmset
requires sudo
: these events affect all users of the computer.
To keep the event but to stop it from being scheduled, uncheck the event’s checkbox in the Schedule view of Power Manager.
Caveats of Scheduling with Power Manager
Power Manager focuses on ensuring the shut down happens. This means applications and user processes are not allowed to block the scheduled shut down.
Well written applications automatically handle this situation but older software may not. In the worst case, the scheduled shut down can lead to unsaved changes being lost; the worst case is when a user has unsaved changes and leaves the computer unattended for a long period and does not see the notifications.
Ideally, no-one should not leave unsaved changes on a unmonitored computer.
Customising Shut Down
So what options remain when the built in approach does not reliably shut down the Mac, and an enforced shut down’s risk of loosing unsaved changes is not acceptable?
You can use Power Manager to schedule your own script or AppleScript to perform the shut down.
There is no requirement to use the Power Manager provided shut down action. If the safer shut down approach does not match your needs, do not use it. You can still continue to benefit from Power Manager’s scheduling, notifications, and user interface.
There are two means of performing a scripted shut down on macOS:
- an AppleScript1 approach;
- the shell script approach.
Let’s create shut down events using these two approaches. Both have limitations.
AppleScript
The AppleScript approach effectively mimics the user selecting Shut Down from the Finder’s Apple menu. The AppleScript reads:
tell application Finder to shut down
Using AppleScript has significant caveats2. AppleScript requires an actively logged in user to work. If no-one is logged in, then the AppleScript will not run and the computer will not shut down.
The AppleScript also suffers from the same problems as the built in macOS scheduled shut down, both can be interrupted by running applications.
Schedule a Shut Down with AppleScript
Let’s schedule a shut down using the AppleScript approach:
Launch Power Manager.app
Add a new event; select the menu item: File > New > New Event…
Select the Schedule Assistant task Run a script daily.
Within the Script step, copy and paste in the two line script below, then…
#!/usr/bin/osascript tell application Finder to shut down
…ensure the Environment is Active User. AppleScripts require an active user to work.
Continue to the When step. Enter the time to shut down and select the desired days of the week.
Continue to the Why step. Name your event and add any notes you desire. The name will be seen by the user of the computer.
Continue to the Confirm step.
Add to confirm the creation of the new event.
Follow notifying the user to add notifications.
The final event will notify the user before performing, provide an opportunity to cancel or delay, and run the AppleScript to begin the shut down.
This approach is not as reliable as Power Manager’s built in shut down action but it more closely matches how macOS normally shut downs. That means applications should store and restore their window states. Running applications should relaunch when the user logs back in.
Shell Script: shutdown
The shell script approach uses the shutdown
command line tool:
sudo shutdown -h now
As discussed in
Shutting Down Your Mac Safely, the shutdown
approach is blunt but is unlikely to be blocked by applications. The shell script will also work even when no-one is logged in. We do not recommend shutdown
in general use; it is effective but is not well adapted to the expectations of Mac applications.
Schedule a Shut Down with shutdown
Let’s schedule a shut down using the macOS
shutdown
command line tool.
Launch Power Manager.app
Add a new event; select the menu item: File > New > New Event…
Select the Schedule Assistant task Run a script daily.
Within the Script step, copy and paste in the two line script below, then…
#!/bin/bash shutdown -h now
…change the Environment to Super User (root). This environment ensures the shut down occurs even if no-one is logged in.
Continue to the When step. Enter the time to shut down and select the desired days of the week.
Continue to the Why step. Name your event and add any notes you desire. The name will be seen by the user of the computer.
Continue to the Confirm step.
Add to confirm the creation of the new event.
Follow notifying the user to add notifications.
Notifying the User
Schedule Assistant’s Run a script daily task creates a quiet trigger. This means no notifications will be shown to the user. Given the implications of shutting down, notifications must be shown to warn the user. Let’s change the quiet trigger to one that notifies:
Within Power Manager.app, select the shut down event.
Open the event in the event editor using the menu item Edit in Event Editor…:
Power Manager.app > Edit (menu) > Edit in Event Editor…
Note the Suppress notifications attribute shown for the trigger; this is what we are changing.
Expand the trigger by through the (…) pop-up menu at the right and selecting Edit… or by double-clicking within the trigger.
Uncheck (deselect) the Quiet option of the trigger.
Apply the trigger changes and Save the event.
The daily trigger will now show a notification to the user before performing the script.
Choices, Caveats, and Complexity
Scheduling a shut down is not trivial. There are choices to be made about what matters most. Do you want to ensure the Mac always shuts down or do you want to safe guard unsaved work at the cost of the schedule.
Thankfully there are options. The four covered here are:
pmset
repeating schedule;- Power Manager’s safer shut down;
- Scheduling
shutdown
; - Scheduling an AppleScript.
Each has its limitations and caveats. Hopefully one will be close enough for you to customise to your needs.
We are aware Power Manager’s safer shut down fills a role but is not yet ideal. Hopefully this recipe has provided some insight into the complexity and choices involved. We are continuing to work on the improving the safer shut down, while maintaining our engineering ideals.