How to remove the 10-minute delay for a scheduled Shut Down

applescriptautomatorcalendarterminal

When I set a Mac used as a public display (reminder: it turns on and off automatically during business hours) to shut down at a specific time (via Energy Saver), macOS will countdown 60 seconds then will add an additional countdown of 10 minutes.

What can I change in macOS so that there is no countdown at all for a scheduled Shut Down?

I found articles on the Net detailing commands for Terminal but they don't manage the 60 seconds/10 minutes countdown.

Somebody told me: "use this AppleScript

osascript -e 'tell application "System Events" to shut down' 

and that will work.

Here's the problem.
I paste that line of code that he gave me in AppleScript Editor, then save it as an AppleScript app (doing it in Automator and creating thus an app is the same).
Then I go to Apple's Calendar, create a daily repeating event and associate an action to open that app.

Now, here's the catch:
If I double-click the app, the Mac is shut down immediately. Fine. But that's not what I need.
If the event is triggered in the Calendar, each time when it opens the app, it says that this is the first time this app is run and if it is OK to open it. Despite the fact that I click on OPEN (or YES I don't remember), this warning appears each time the app is launched. It seems that the Shut Down process happens so fast that macOS does not have the time to put it in the registry of authorized apps.

So the question is: how to launch that line of code —without buying the $50 PowerManager— at a specific time? Because when that line of code is run not from an AppleScript or Automator app, it works (no warning about the app).

Thank you.

Best Answer

You don't need paid apps to do this as it's very simple using Bash (the default shell in Terminal).

You can have the computer shutdown by issuing the command in Terminal

$ sudo shutdown -h now

If you try it, it will shutdown your Mac immediately.

What each part does

  • sudo = run as the root user
  • shutdown = the command to shutdown the system
  • -h = halts the system
  • now = immediately

So, if you wanted to shut the computer down 5 minutes from now, your command would be:

$ sudo shutdown -h +5

You can even schedule it for a reboot (-r instead of -h) at a particular time (11:00pm every night)

$ sudo shutdown -r -h 23:00

While that command all by itself, would do what you want, it's limited because of sudo; you'll need to enter your password when it's run. This means you'll have to be on-prem at each machine each time you want to issue the command (every day), so we need something that will run the command as root so it will be automatic.

Writing a Bash Script

The script for this is quite simple; it's a single line that calls the shutdown command:

#!/bin/bash

/sbin/shutdown -h now

Call it auto_shutdown.sh and save it to a convenient location. Make it executable by issuing the command:

$ sudo chmod +x auto_shutdown.sh

This will set the execute bit and allow the system to process it as a program. Now you have your script, you just need something to execute that script at a predetermined time.

Using Launchd to schedule your job

launchd is the service management framework that starts/stops/restarts processes, services, daemons, etc. To set up a job to kick off with launchd you need two items:

  • type of job to be run; run as the user for one user, as the user for all users, or a job run by the system as root. We want the last one.
  • plist (XML file) that describes the job. The key elements that you need to focus on right now are the ProgramArguments that calls the script and the CalendarInterval which specifies when the jobs should be run. For this example, call your plist user.com.autoShutdown.plist (no other reason for that naming convention other than it follows Apple's conventions.)

    <?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.user.autoShutdown</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Path_to_Script/auto_shutdown.sh</string>
        </array>
        <key>StartCalendarInterval</key>
        <array>
        <dict>
            <key>Hour</key>
            <integer>21</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
    
    </dict>
    

This plist is set to run the auto_shutdown.sh script every day at 11:30pm.

Since we want to run this as root, copy the plist to the /Library/LaunchDaemons directory.

$ sudo cp com.user.autoShutdown.plist /Library/LaunchDaemons

Finally, load the job into launchd:

$ sudo launchctl load com.user.autoShutdown.plist

That's it. Every day at 11:30, the system will automatically shutdown.

Replicate across all your machines

Copy both the script and plist to each machine and load the job into launchd. You have just set the shutdown time for all machines and it didn't require 3rd party ($$$) utilities.


You can find info on all the commands listed here by looking up their man page. Type man <command> for the command you're interested in.