Help with script to keep Mac awake

Discussion in 'Mac Programming' started by Donka, Aug 11, 2015.

  1. Donka macrumors 68020

    Donka

    Joined:
    May 3, 2011
    Location:
    Scotland
    #1
    Hi,

    I have an issue with my Mac sleeping after the set period when there is a background process running - currently set to 15 mins. While this process is running, which could be anything from 20 mins to a couple of hours, I wish to keep the Mac awake.
    There is a log file which is regularly updated while the process is running so I'm thinking if there is a way of checking the timestamp of this log file on a regular basis. If it has changed for example in the last 10 mins, perform some action that will keep the Mac awake e.g. invoke caffeine or mimic a cursor movement or similar?

    Does anyone have any thoughts or ideas on how to achieve this? I'm new to this kind of thing but reasonably tech savvy and happy to learn.
     
  2. MasConejos macrumors regular

    MasConejos

    Joined:
    Jun 25, 2007
    Location:
    Houston, TX
    #2
    It's note quite what you want, but it's probably easier then messing with a script: Caffeine will keep your mac awake indefinitely or for a set period of time.
     
  3. Donka thread starter macrumors 68020

    Donka

    Joined:
    May 3, 2011
    Location:
    Scotland
    #3
    Thanks - I already use caffeine but I want something to work in the background automatically as I will not be at the mac at the time - it will be running as an on demand server. I have found a couple of possible app solutions that I will try out tonight - Jiggler and Wimoweh.
     
  4. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #4
    Have you seen caffeinate(8)? If you can run your background process with it it should be trivial. Otherwise you could do some horrible hack like:

    Code:
    caffeinate -i sh -c 'while [ $(( `date +%s` - `stat -f '%m' logfile.log` )) -lt 300 ]; do sleep 10; done'
    
     
  5. Donka thread starter macrumors 68020

    Donka

    Joined:
    May 3, 2011
    Location:
    Scotland
    #5
    Thanks - I read briefly about that today and I suspect any script would feature that but I will hold off until I can test these two apps I found tonight. If the process spawns a new process, I should be able to use the apps to keep awake until that process finishes. If it doesn't spawn a new process but the parent process uses additional CPU then I should be able to keep awake while that process's CPU is above a specific threshold.
     

Share This Page