3

I have the following scenario. I have shell scripts that I want to run remotely on my mac pro, so I ssh in, launch them with nohup, and log out. I've done this for years on linux based machines, and I just started trying it out on the new mac pro. However, I find that the mac goes to sleep while these are running, and it takes much longer to finish. Obviously I can disable sleep in the energy preferences, but there are definitely times when I want it to let it power down. Is there anything I could do, in terms of a command line argument, that would disable sleep while the process was running?

3

caffeinate your scripts

Use the OS X built-in caffeinate command to prevent your Mac going to sleep. caffeinate is the recommended way of lodging a temporary restriction against your Mac entering a low power state.

caffeinate creates assertions to alter system sleep behavior. If no assertion flags are specified, caffeinate creates an assertion to prevent idle sleep. If a utility is specified, caffeinate creates the assertions on the utility’s behalf, and those assertions will persist for the duration of the utility’s execution. Otherwise, caffeinate creates the assertions directly, and those assertions will persist until caffeinate exits.

2

You can use pmset to temporarily prevent mac from going to sleep.

pmset noidle & PMSETPID=${!};sleep 10; kill $PMSETPID

Replace sleep 10 with your command.

You can also write a function and save it to .bash_profile.

insomnia() {
    pmset noidle &
    PMSETPID=$!
    $($@)
    kill $PMSETPID
}

and use it like this: $ insomnia sleep 10

I recommend to use screen instead of nohup.

Edit: on newer Macs you should use caffeinate as described by Graham Miln. pmset noidle still works, but since 10.9 it is marked as "deprecated" in the documentation.

0

I made a function inside my ~/.bash_profile, like so:

# ~/.bash_profile

# wrap ssh so computer doesn't go to sleep
ssh()
{
    echo "caffeinating ssh session..."
    LC_CTYPE= # fix issue with UTF8 on server
    caffeinate -i ssh "$@"
}

Now when I use my "common" the ssh command it get wrapped in caffeinate therefore keeps the system from going to sleep.

Now when i start a ssh session, i can see pmset -g telling me:

sleep                10 (sleep prevented by caffeinate)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .