A tiny macOS shell script for toggling system sleep with pmset.
It is intended for cases where you need a system-level sleep toggle, including lid-closed use, rather than a temporary per-process keep-awake command.
chmod +x nosleep.sh
./nosleep.sh on
./nosleep.sh off
./nosleep.sh status
./nosleep.sh setup
./nosleep.sh helponrunssudo pmset -a disablesleep 1offrunssudo pmset -a disablesleep 0statusreads the currentdisablesleepvalue frompmsetsetupinstalls a sudoers rule soon/offstop asking for a passwordhelpshows the usage message
- Running a long local job or server while the Mac is closed and away from a charger.
- Allowing longer-running work such as app builds, model training, or other compute-heavy tasks to continue while you are in transit or moving between meetings.
- Keeping a remote session, file transfer, or other background task alive when normal sleep would interrupt it.
- Temporarily preventing sleep on a machine used as a small home server, lab machine, or automation host.
- Using a Mac in a setup where
caffeinateis not sufficient and you intentionally need sleep disabled at the system level.
caffeinate is commonly used to keep a Mac awake while a process is running or while the display remains open, but it does not change the underlying system sleep setting in the same way.
This script uses pmset to change the system disablesleep setting directly. In practice, that means it can continue preventing sleep in situations where caffeinate is not the right tool, including lid-closed use and operation on battery power without a charger connected.
- This changes a system-level power setting, not just a single terminal session.
- If left enabled, your Mac can remain awake with the lid fully closed and while not connected to power.
- This can increase battery drain and may cause the laptop to become hot, especially if it is placed in a bag or another poorly ventilated space.
- Use it only when you intentionally need this behavior, and run
offas soon as you are done.
- macOS only
onandoffusesudo, so they may prompt for an administrator password- running the script with no arguments shows the usage message
-aapplies the setting to all power profiles
If you want to call it as nosleep from anywhere:
chmod +x nosleep.sh
ln -s "$(pwd)/nosleep.sh" /usr/local/bin/nosleepThe install step usually does not require a password, but creating the symlink may need one if /usr/local/bin is not writable by your user.
The easiest way:
./nosleep.sh setupThis creates a sudoers drop-in file for the current user. You'll enter your password once during setup — after that, on and off work without prompting.
You can also do it manually for a single user or an entire group:
# Allow all admin-group users to run pmset without password
echo "%admin ALL=(ALL) NOPASSWD: /usr/bin/pmset -a disablesleep 0, /usr/bin/pmset -a disablesleep 1" | sudo tee /etc/sudoers.d/nosleepOr limit it to a single user:
echo "yourusername ALL=(ALL) NOPASSWD: /usr/bin/pmset -a disablesleep 0, /usr/bin/pmset -a disablesleep 1" | sudo tee /etc/sudoers.d/nosleepThis creates a drop-in file under /etc/sudoers.d/, which is safer than editing /etc/sudoers directly. The status command does not use sudo, so it is unaffected.