Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Can I disable the sleep mode for a specific application so that my mac won't go on sleep until the app is closed?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

If you have any interest in running shell script the following will work. Just set the "Display_Sleep_Time" and "System_Sleep_Time" as you want them to be along with the "App" you are choosing to prevent sleep on. By launching the script anytime you run your app your all sleep times will be turned to 0 preventing both your display and system from sleeping.

TESTED ON

  • 10.5.x
  • 10.6.x
  • 10.7.4

NOTE: WARNING THIS SCRIPT DOES CONTAIN AN INTERNAL LOOP. You may prefer to utilize the following script inside a Daemon rather than use the internal loop the script provides. However, the same concept applies.

#!/bin/bash

watch="true"

App="Safari"
System_Sleep_Time="30"
Display_Sleep_Time="10"

System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')

while [[ ${watch} == "true" ]]; do

    watch_App=$(ps -A | grep "${App}.app" | grep -v grep | sed "s/.*${App}.*/${App}/")

    if [[ ${watch_App} == ${App} ]]; then

        if [[ ${System_Sleep} != "0" ]]; then
            pmset -a sleep 0
            System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
        fi

        if [[ ${Display_Sleep} != "0" ]]; then
            pmset -a displaysleep 0
            Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')
        fi

    elif [[ ${watch_App} != ${App} ]]; then

        if [[ ${System_Sleep} == "0" ]]; then
            pmset -a sleep ${System_Sleep_Time} > /dev/null 2>&1
            System_Sleep=$(pmset -g | grep " sleep" | awk '{print $2}')
        fi

        if [[ ${Display_Sleep} == "0" ]]; then
            pmset -a displaysleep ${Display_Sleep_Time} > /dev/null 2>&1
            Display_Sleep=$(pmset -g | grep " displaysleep" | awk '{print $2}')
        fi

    else

        /usr/bin/logger -i An error has occured with preventsleep.sh

    fi

done
share|improve this answer
    
Could you add to the answer how to setup a deamon? –  dierre Apr 19 '12 at 16:47
    
The simplest way might just be to download an app to help you through it. If you google 'download mac daemon tool free' you should find some helpful applications. They are generally very small and very simple to follow. Don't be afraid to try them. Remember though, it is not 100% necessary to do this. You can create a daemon without the aid of an application, or you can run this script as needed and if you are running it with root privileges it will function adequately. –  E1Suave Apr 19 '12 at 17:18
    
Sorry for being a bit vague in regards to the downloads, but I am not 100% certain what the Stack Exchange policy is for listing specific applications to download. –  E1Suave Apr 19 '12 at 17:20
1  
developer.apple.com/library/mac/#documentation/MacOSX/… found it. Tnx. Nice answer btw. –  dierre Apr 19 '12 at 17:38
add comment

You could also add a line like this to a crontab:

* * * * * pgrep -x TextEdit && caffeinate -t 60

(Use EDITOR=nano crontab -e if you haven't changed $EDITOR and don't know how to use vi.)

caffeinate and pgrep were both added in Mountain Lion.

You can get a list of power assertions with pmset -g assertions. caffeinate or caffeinate -i should set PreventUserIdleSystemSleep to 1.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.