Problem overview
By default, macOS may allow a device to enter sleep mode even when it’s connected to a power source, disrupting remote sessions, ongoing tasks, or important server functions. This script addresses that challenge by preventing unwanted sleep events while the device is plugged into AC power, ensuring smooth, continuous operation.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-macos-disable-sleep-on-ac-power
echo "Disabling macOS sleep settings when plugged into power (AC)..."
# Disable system sleep when on AC power
sudo pmset -c sleep 0
# Prevent disk sleep when on AC power
sudo pmset -c disksleep 0
# Prevent idle sleep when on AC power
sudo pmset -c disablesleep 1
# Prevent system from hibernating when on AC power
sudo pmset -c autopoweroff 0
# Prevent the system from entering standby mode when on AC power
sudo pmset -c standby 0
# Verify changes
echo "Updated power settings for AC power:"
pmset -g
echo "Sleep has been disabled while plugged in."
This script uses the pmset command to adjust power management settings in macOS specifically for AC power usage. It disables system sleep, disk sleep, idle sleep, auto power off, and standby mode when the Mac is plugged in. After applying these new settings, it provides a confirmation by displaying the updated power configuration, giving you immediate visibility into the changes made.
Use cases
- Maintaining reliable remote access to headless or unattended Mac servers
- Ensuring background tasks and processes remain uninterrupted
- Running continuous workflows or kiosk sessions without downtime
- Preventing any unexpected sleep during critical updates or backups
Recommendations
- Test this script on a non-production or test device to confirm it applies correctly
- Use Level to run this on newly added devices or as part of a setup automation to ensure you have consistent remote access
- If you need this applied regularly, create an Automation in Level with a scheduled trigger to ensure the settings remain enforced