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
If you try it, it will shutdown your Mac immediately.
What each part does
sudo
= run as the root usershutdown
= the command to shutdown the system-h
= halts the systemnow
= immediatelySo, if you wanted to shut the computer down 5 minutes from now, your command would be:
You can even schedule it for a reboot (
-r
instead of-h
) at a particular time (11:00pm every night)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 asroot
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:
Call it
auto_shutdown.sh
and save it to a convenient location. Make it executable by issuing the command: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 joblaunchd
is the service management framework that starts/stops/restarts processes, services, daemons, etc. To set up a job to kick off withlaunchd
you need two items: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 theCalendarInterval
which specifies when the jobs should be run. For this example, call your plistuser.com.autoShutdown.plist
(no other reason for that naming convention other than it follows Apple's conventions.)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.Finally, load the job into
launchd
: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.man shutdown
man launchd
man sudo
man launchctl