Mac – Stop a Mac from sleeping while a bash script is running, then allow it to sleep as normal once complete


I've got my Mac to wake up during the night and run an rsync to back it up. This is configured through Energy Saver > Schedule.

However it seems that it's going back to sleep before it can get anything done, so I need to stop it sleeping during the execution of the bash script that rsync runs from.

I figured the best way (if it's possible) is to issue a command to set the sleep timeout to "Never" or a very long timeout before it rsyncs and then back to normal once it's complete. Is there a better solution for doing this?

Best Answer


For example:

caffeinate -i rsync -avz someuser@somehost:somefolder /some/local/folder

From the man page:

     caffeinate -i make
        caffeinate forks a process, execs "make" in it, and holds an
        assertion that prevents idle sleep as long as that process
        is running.

See man caffeinate for details.