Help with caffeinate

Due to the sleep behavior of 10.8 affecting several of my operations, I have been trying to use caffeinate but have some questions. What I need is for some applications to continue working without my input (ie: large downloads, lengthy disk operations, time machine, etc.) but have the system sleep at the regular interval set in Energy Saver preferences, when those apps are not busy.


This is an example of my result when trying to use caffeinate, the application is open, I am admin, tried as sudo with same result:

macbook-pro:~ user$ caffeinate -i /Applications/Vuze.app/
/Applications/Vuze.app/: Permission denied

Why would be permission denied? Do I need to add arguments, what would those be?


Is there any way to set caffeinate once and have it work on the specified application every time I open it, without having to turn on caffeinate every time I need it?

MacBook Pro, OS X Mountain Lion

Posted on Jul 30, 2012 2:46 PM

Reply
Question marked as ⚠️ Top-ranking reply

Posted on Aug 16, 2012 2:26 AM

I have the answer to this, supplied by folks on the Usenet group comp.sys.mac.system. It can also be found elsewhere, but without explanation.


The point is that caffeinate, as a Unix command, can't take a Mac application as argument, but only another Unix program.


The Unix command 'open' can be used to open a Mac program (and can be applied without problems even if the Mac program is already running).


caffeinate -i open ThisApp.app


is not good enough, as the open command does its job and then quits, so caffeinate is no longer kept open.


But


caffeinate -i open -W ThisApp.app


keeps the open command alive (and so caffeinate will be active) until you quit ThisApp.


I am told that the syntax should be


caffeinate -i open -W -a ThisApp.app


but I am not sure what -a does.


If ThisApp is in the Applications folder, this is all that is needed, otherwise you have to specify the full path.


As for your last question, I suggest a shell script, but I have not yet tried it. Also, if you set the shell script to open a dummy application (something whose only function is to stay open until manually quit, which can easily be done by an Applescript) you would have something that, when run, would keep the machine awake until the dummy program was quit, independent of whatever else was running.

4 replies
Sort By: 
Question marked as ⚠️ Top-ranking reply

Aug 16, 2012 2:26 AM in response to hochojin

I have the answer to this, supplied by folks on the Usenet group comp.sys.mac.system. It can also be found elsewhere, but without explanation.


The point is that caffeinate, as a Unix command, can't take a Mac application as argument, but only another Unix program.


The Unix command 'open' can be used to open a Mac program (and can be applied without problems even if the Mac program is already running).


caffeinate -i open ThisApp.app


is not good enough, as the open command does its job and then quits, so caffeinate is no longer kept open.


But


caffeinate -i open -W ThisApp.app


keeps the open command alive (and so caffeinate will be active) until you quit ThisApp.


I am told that the syntax should be


caffeinate -i open -W -a ThisApp.app


but I am not sure what -a does.


If ThisApp is in the Applications folder, this is all that is needed, otherwise you have to specify the full path.


As for your last question, I suggest a shell script, but I have not yet tried it. Also, if you set the shell script to open a dummy application (something whose only function is to stay open until manually quit, which can easily be done by an Applescript) you would have something that, when run, would keep the machine awake until the dummy program was quit, independent of whatever else was running.

Reply

Aug 16, 2012 4:55 AM in response to danco34

I participated in the aforementioned discussion on comp.sys.mac.system and supplied the details on how the command syntax worked.


Thanks to a hint from Wes Groleau we spotted the -W argument to the open command, which tells the open command to wait until the application quits before the open command itself terminates.


The -a argument to the open command means "the next parameter is the name of the application to open". Without -a, the next parameter is interpreted as the pathname of a folder, document or application to open. (If a document is specified, the standard LaunchServices mechanisms are used to determine the default application to launch.)


If you open an application using this form:


open -W ThisApp.app


the open command expects to find ThisApp.app in the current folder. It won't look in other folders, such as the Applications folder. The correct syntax here would typically be:


open -W /Applications/ThisApp.app


If you use the -a option, the next parameter is interpreted as the name of an application, and the open command looks for the application in standard places like the Applications folder. You also don't need to enter the .app suffix (but it will work either way).


You can also compress the options slightly, specifying -W and -a in a single parameter.


Thus the shortest form of the whole command would be:


caffeinate -i open -Wa ThisApp


(assuming ThisApp is the name of an application in your Applications folder).


As to the question of how to have caffeinate always take effect while a target application is running: two general methods occur to me.


(a) Rather than launching the target application directly, write an AppleScript or Automator script saved as an application, which launches the target application via caffeinate. The script application would remain running unti you quit the target application.


(b) Write a shell script and associated launchd script which is set up as a LaunchAgent to run on a periodic basis. The shell script checks to see whether the target application is running, and if so, executes the caffeinate command above to keep the system from going idle while the target application is running.


Method (b) is tidier, as it doesn't have another visible support application running, doesn't require the user to change how they launch the target application, and allows for applications which get launched via methods such as opening a document.

Reply

Aug 16, 2012 12:16 PM in response to David Empson

Yes, indeed, many thanks David (and Wes). I hope you weren't offended that I had not mentioned you by name; I did indicate that the solution was not mine, and where it could be found.


Thanks for explaining the -a parameter. It was not relevant when I tried, as I used drag and drop to put the application on the line in Terminal, so I was using the full path. But I would never have guessed that was what -a did from the manual reference.


In Snow Leopard I kept my system awake whatever application was being run by using Jiggler or Caffeine. So my personal preference in Mountain Lion is for a shell script that runs a dummy application that does nothing but stay open until it is quit (for instance, the Applescript "delay 10" saved as a stay-open application, though I expect there are better ways to get such a dummy application). With a bit of extra effort the script could hide the dummy application.


I expect that, now caffeinate is better understood, someone (you, David? or Wes? - certainly not me) can create a GUI utility like Caffeine that can just be turned on and off by a click on a menu bar item. I'll email the developer of Caffeine.

Reply

Aug 17, 2012 3:58 AM in response to danco34

OK, I now have a procedure that works to turn off sleep. I am sure my script can be improved. And a good programmer could make it into a toggle. Here it is (use at your own risk, not that I think there is much risk).


1. create a dummy program that does not do anything except stay open, and keep it with the name Dummy in the Applications folder. I used the Applescript


delay 10


saved as a stay-open application.


2. Create a shell script in the usual way. The script should be


#! /bin/bash

osascript -e 'tell application "System Events" to tell process "Terminal" to set visible to false'

caffeinate open -Wga Dummy


Now when the shell script is double-clicked, it opens a Terminal window, and the first line calls an Applescript to hide Terminal. The second line has been discussed by David. It opens Dummy in the background (the -g flag). Caffeinate does not need -i as that is the default. You might want to use -di, which prevents both display and ilde sleep (or even -s, which prevents system sleep).


When you quit Dummy, caffeinate stops. There will be a Terminal window in the background that can be closed or Terminal can be quit, as you like.


I can work out the logic of a more complicated script that does the above if Dummy is not already running while if Dummy is running it quits it, closes the Terminal window for the script, and quits Terminal if it is not doing anything, but I would not dare to actually code that scirpt.

Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Help with caffeinate

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.