I‘m running a process to complete backup and covert images in Mac OS X laptop. I do not wish to disable sleep manually from my OS X power options (Energy Saver). How do I temporarily prevent a Mac from sleeping while my process is still active in Mac OS X Unix operating systems? How can I prevent the system from sleeping temporarily from the command prompt?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements OS X
Est. reading time 2m
[/donotprint]You can use the caffeinate command. This command creates assertions to alter system sleep behavior. If no assertion flags are specified, caffeinate creates an assertion to prevent idle sleep.

Another option is to use the pmset command to manipulate power management settings.

Fig.01 Prevent System Sleep While a Safari Command is Active in Mac OS X

Fig.01 Prevent System Sleep While a Safari Command is Active in Mac OS X


The syntax is:

caffeinate [options]
caffeinate [options] command


Let us see how to use the caffeinate command. Open the Terminal application and use the command as per your need.

Prevent the display from sleeping

$ caffeinate -d

Prevent the system from idle sleeping

$ caffeinate -i

Prevent the disk from idle sleeping

$ caffeinate -m

Prevent the system from sleeping when system is running on AC power

$ caffeinate -s

How do I declare that user is active?

$ caffeinate -u vivek
$ caffeinate -u vivek wget url

If the display is off, this option turns the display on and prevents the display from going into idle sleep. If a timeout is not specified with the -t option, then this assertion is taken with a default of 5 second timeout.

How do I set timeout?

$ caffeinate -t 60
You can specifies the timeout value in seconds for which this assertion has to be valid. The assertion is dropped after the specified timeout. Timeout value is not used when an utility is invoked with this command (120 seconds time out is not valid as wget command invoked):
$ caffeinate -t 120 wget url

Prevent sleep as long as given process is running

In this following example, caffeinate command forks a process, execs “make” in it, and holds an assertion that prevents idle sleep as long as that process is running:

caffeinate -i make
# Avoid disk from idle sleeping
caffeinate -m wget url
# Avoid display from idle sleeping while watching netflix (just in case)
caffeinate -m /Applications/Firefox.app/Contents/MacOS/firefox-bin https://netflix.com

Not a fan of command line?

  1. Click on Apple menu > System Preferences, then click Energy Saver.
  2. Drag the slider that controls the computer’s sleep to Never.
  3. When done again restore to the defaults.

Fig.02 Prevent your Mac from sleeping using GUI method

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum