Prevent System Sleep While a Process or Command is Active in Mac OS X

Jun 16, 2014 - 1 Comment

terminal Many Mac users use utilities to temporarily prevent their computer from sleeping, often relying on sleep corners, the third party tool called Caffeine, pmset, or more recently, the command line utility that is now bundled with OS X called caffeinate. By default, the Caffeine menubar item and the caffeinate command both will prevent sleep as long as they are individually activated for, very useful for desktop users while at a Mac, but not particularly useful if you’d like to have the OS X sleep function dependent on the completion of a specific process or task.

That’s what we’re going to cover here by using the command line, which is having process-dependent sleep prevention that only blocks the Mac sleep function while a specified command, task, or process is running or active, then when it’s finished, the computer will restore traditional sleep habits.


To accomplish command specific sleep avoidance, we’re going to use a variation of the caffeinate command, which can prevent sleep in a variety of ways. We’ll run through a few examples but the man page of caffeinate provides a few additional options which may be desirable for other situations too.

Caffeine prevents sleep while a command is running

For our purposes here to make the sleep prevention dependent on the completion of a specific command or process, you’ll use the -i flag likes so:

caffeinate -i [command / process]

It may already be obvious, but executing the caffeinate command like this will also start the command or process specified in the argument.

For example, if you wanted the Mac to avoid sleeping when the “make” command is active, you’d use the following command syntax:

caffeinate -i make

Or perhaps you just want to prevent sleep while an ssh connection is active to a specific server, and you want to send the caffeinate command into the background, then you’d apply an ampersand to the end as well like so:

caffeinate -i ssh coffeebeans &

You can even run this with your own scripts or commands at another location:

caffeinate -i /private/tmp/./whatisthis.sh

Or to have sleep prevention active so long as the Safari web browser is running in the GUI, you’d use the following syntax, note you must specify the full path to the binary within the .app file:

caffeinate -i /Applications/Safari.app/Resources/MacOS/Safari

The -i flag prevents system sleep, but you can use other flags to create assertions for preventing disk or display sleeping as well. To prevent display sleep (that is, the screen turning off and going into locked mode or a screen saver), the -d flag is needed:

caffeinate -d [command / process]

Like the -i flag this can apply to any system task, whether it’s run from the command line or the GUI, just be sure to specify the proper name with proper case. Another example:

caffeinate -d telnet towel.blinkenlights.nl

That command means that as long as telnet to the Star Wars ASCII movie is active, the Macs display will not sleep. If telnet is quit or Star Wars is finished playing, the system may sleep as is usually allowed, defined by sleep and energy settings in OS X.

Of course since caffeinate is entirely based in the command line it’s not going to be applicable to all users, but for those who spend a lot of time in the Terminal, it can be a great trick. For Mac users who are more comfortable in the graphical user interface, an effective anti-sleep corner and using the Caffeine app are still probably the best bets to perform similar functions.

If you want to perform a similar process-dependent task but avoid the Terminal and command line, the Wimoweh app accomplishes the same thing as a menu-bar drop down, though it is a paid app which may make it less desirable for some users, and it does not offer a general process or command line based task completion argument. Thus, caffeinate with a proper flag is still preferred by many.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

One Comment

» Comments RSS Feed

  1. Rene R says:

    There’s a tool called CaffeinateMe which has been created around the caffeinate shell command: http://renerosendahl.com/software/software/caffeinateme

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates