You might consider making a Stay-Open Applescript Application that sets/resets the sleep timer based on the existence of a running process. Creating a launchd plist is also a viable solution, however I'm still shaky on the syntax. The "pmset force sleep X" command doesn't require root access, but the settings are reset on reboot.
Since your situation sounds like I won't be able to anticipate your every need, I'll sketch something out for you.
property LoopTime: 5 --measured in seconds
property normalSleepTimeout: 30 --measured in minutes
property processName: "rsync" --the name of the process you're trying to find
on run
do shell script "pmset force sleep 0" --disables sleep
idle()
end
on idle
if not appIsRunning() then
do shell script ("pmset force sleep " & normalSleepTimeout as string) -- sets sleep to desired idle timeout
quit
end
return
end
on appIsRunning()
--Here's where you need to do the test that an app is running. True needs to mean "the app is running". Store the value to "result" or change the below return statement.
return result
end
For things like rsync and background processes, you'll need to get more clever and poll other functions like $ top.
set result to False
if 0 < (count of (do shell script ("top -l 1 | grep" & processName as string))) then
set result to True
end
Notice that in the above case, searching for just "rsync" will return a false positive if rsyncd is running because both "rsync" and "rsyncd" match. You may have to get more tricky if this doesn't work for you.
If the application were a Windowed process, I'd use the following to determine what's running:
tell application "System Events" to set RunningAppNames to name of processes
Or for bundle identifiers (more precise)
tell application "System Events" to set RunningBundles to bundle identifier of processes
Tell me more about your scenario and I'll try to write something more exact and with a more flexible user interface.
2This is insanely awesome! Is this default on any OSX? Since which version? (I couldn't quickly find info about this) – cregox – 2014-09-04T15:49:30.300
@Cawas - according to this article, it started shipping with Mountain Lion: http://www.cnet.com/news/caffeinate-mountain-lion-to-prevent-it-from-sleeping/ I found out about it from
– Nathan Long – 2014-09-04T18:02:10.760man pmset
, where thenoidle
option is listed as deprecated.Also, see this nice tutorial: http://computers.tutsplus.com/tutorials/quick-tip-how-to-stop-your-mac-from-sleeping-using-the-command-line--mac-50905
– Nathan Long – 2014-09-04T18:08:18.067