# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action.

Scripting language – Bash

File extension – .sh

 Disclaimer: 
The sample scripts provided below are adapted from third-party open-source sites.

 

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.

Script to put Mac to sleep

sudo pmset sleep 15 

   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.

Script to set display sleeptime on Mac

sudo pmset displaysleep 15 

   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.

Script to schedule wake Mac

sudo pmset repeat wakeorpoweron MTWRFSU 9:00:00 

   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.

Script to prevent sleep for a specific period of time

caffeinate -u -t 300 

   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.

Script to check system sleep idle time

sudo systemsetup -getcomputersleep 

   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.

Script to turn off system sleep

sudo systemsetup -setcomputersleep Never

   1

  sudo systemsetup -setcomputersleep Never

   

 

  

This command turns off the system’s sleep function.

Script to restart on system freeze

systemsetup -setrestartfreeze on 

   1

  systemsetup -setrestartfreeze on 

   

 

  

This command ensures that the system automatically restarts if it freezes.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.