Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a long running script at the remote computer.

I don't want terminate my ssh session, and need enter it into a shell script.

How is it possible to prevent my Mac from falling into sleep from the command line?

share|improve this question
Which OS X version? – patrix Aug 30 '12 at 20:47
up vote 17 down vote accepted

In Mountain Lion you can use the caffeinate command.

caffeinate -u -t 1000

will prevent idle sleep for 1000 seconds.

share|improve this answer
I've had no success yet with any 'caffeinate' cmdline combination (including one above) nor in Mavericks preventing sleep upon lid close. is only thing that has thus far prevented lid sleep, but haven't yet found a working cmdline version (haven't yet made work - it may be obsolete). Even still, I want something that enables display-password requirement even while denying sleep for limited, programmable time upon lid close...and doesn't appear to do that either. – Johnny Utahh Feb 11 '14 at 20:08
Similar issues. I am on Yosemite, and even though I run caffeinate in terminal, after a while the screen goes dark, and I have to log back in. This suggests to me that caffeinate isn't keeping the Mac awake, which is my goal so that the Wifi connection can persist / whatever I have going on can proceed. – dmonopoly Jan 12 '15 at 3:39

The solution to this problem is not keeping the client (your Mac) awake. Using approaches like this are undependable. What happens if the network connection is lost? Even if your Mac is awake, the script will halt.

Use nohup

If your long-running script is called, try the following:

nohup /path/to/ > /path/to/output.out &

Now you can even close the connection and your script will keep running. The & backgrounds the process so you can keep the connection open and enter commands. View any output from your script via:

tail -f /path/to/output.out

The paths in the examples are optional if the script is on your path and you want script output to be written to output.out in the current directory.

I manage scripts that run for days at a time. Scripts like these should be detached from the terminal. Thankfully, nohup provides an easy-to-remember command invocation to achieve this--think no hangup.

share|improve this answer
See also man screen. – user588 Sep 1 '12 at 6:20
@mankoff - i'm already using screen. It is the best command since the dicovery of a sliced breed. ;) The main point of the question is eleiminate the need of the "ssh to@somewhere" when my mac fall asleep - I simply don't want terminate my ssh session - my command on the remote side is already "protected" with screen. ;) – kobame Sep 3 '12 at 11:40
@ephsmith - the question was: how to eliminate the ssh session terminate... ofc nohup is nice for situations when your remote command is not an interactive one. For the interactive commands the screen is much much better solution. Thanx anyway for reply. – kobame Sep 3 '12 at 11:43
@kobame, maybe the point of the question and this additional information should've been included in your original question. The first line in your question referred to a long running script. I assumed it wasn't interactive which led to the reference to nohup. – ephsmith Sep 3 '12 at 13:31
@kobame, did any of these answers solve your problem? – ephsmith Sep 3 '12 at 13:32

Another option is pmset. Use the command pmset noidle to prevent sleep as long as pmset is running. Unfortunately, it requires a separate Terminal window with pmset running in it. However, the other option, caffeinate, only lets you set a certain time. So it's a matter of choosing whether you want to open a second SSH session, or deal with a time restraint.

Edit: According to binarybob's comment, you can actually run it in the background like this: pmset noidle & To get back to pmset type fg.

share|improve this answer
You can also run pmset in the background using pmset noidle & and therefore continue using the current command prompt. Type fg to get pmset back, at which point you can Ctrl+C it – binarybob Aug 31 '12 at 9:21
@binarybob Oh! I didn't know that. Thanks for the tip! – daviesgeek Aug 31 '12 at 14:26
@davisgeek - really nice! Thanx for the tip. – kobame Sep 3 '12 at 11:45
@kobame Yup. NP. That's what we're here for! :-) – daviesgeek Sep 3 '12 at 17:48
caffeinate -i -s /bin/ssh ...


-i - Prevent idle sleep.

-s - Prevent system sleep (entirely, even if you close the lid). Note: it only works while on AC power.

/bin/ssh - Just keep writing the command you want to execute. Using ssh directly instead of /bin/ssh should also work.

Results: Your system will not sleep as long as the ssh command is running.

share|improve this answer

If you are looking for the nuclear option like I was:

sudo systemsetup -setcomputersleep Never

Set or Disable Sleep Due to Mac System Inactivity from the Command Line in OS X

share|improve this answer

The StarMessage screensaver ( ) has also the option to keep the MAC awake for 2 hours, 8 hours, or permanenty. This way of disabling the MAC sleep, is much easier than caffeine.

share|improve this answer
How does this run for the command line? – Mark Feb 28 at 13:16
Be sure to read the promotion section on Help Center. – patrix Feb 28 at 14:01

Your Answer


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.