Category filter
Script to configure sleep settings for Mac
Users will typically select the Apple icon from the menu bar and select Sleep to put a device in the sleep state. To configure the display sleep settings, they will have to open System Preferences > Battery. From there, they may set the Turn display off after duration and schedule sleep/wake-up events. Device admins might want to configure these options remotely with the below shell scripts.
You can remotely run scripts on Macs managed with Hexnode using the Execute Custom Script action.
Scripting language – Bash
File extension – .sh
Manage power settings on Mac
With the power management settings pmset
command, we can manage settings like idle sleep timing, wake on administrative access, automatic restart on power loss, etc.
1 |
sudo pmset sleep 15 |
This command will set the system sleep timer to 15 minutes i.e., the system will automatically sleep after 15 minutes of inactivity.
1 |
sudo pmset displaysleep 15 |
This command will set the display sleep timer to 15 minutes i.e., the system will turn off the device display after 15 minutes of inactivity.
1 |
sudo pmset repeat wakeorpoweron MTWRFSU 9:00:00 |
This command will wake or power on the system every day at 9:00 am. Here, MTWRFSU represents the weekdays as –
M – Monday
T – Tuesday
W – Wednesday
R – Thursday
F – Friday
S – Saturday
U – Sunday
Examples –
-
sudo pmset repeat shutdown F 20:00:00
This command will shut down the system every Friday at 8:00 pm.
-
sudo pmset repeat cancel
This command will cancel all repeating
pmset
events. -
sudo pmset –g
This command will return the system-wide power settings.
1 |
caffeinate -u -t 300 |
This command will prevent the system from sleeping for 300 seconds from the time it is executed. The time is specified in seconds here.
1 |
sudo systemsetup -getcomputersleep |
This command will report the number of idle minutes the system waits before it sleeps. If the output is Never, it indicates that the system’s sleep function is off.
1 |
sudo systemsetup -setcomputersleep Never |
This command turns off the system’s sleep function.
1 |
systemsetup -setrestartfreeze on |
This command ensures that the system automatically restarts if it freezes.