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?
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?
In Mountain Lion you can use the caffeinate
command.
caffeinate -u -t 1000
will prevent idle sleep for 1000 seconds.
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 eternity.sh
, try the following:
nohup /path/to/eternity.sh > /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.
man screen
.
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
.
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
Commented
Aug 31, 2012 at 9:21
!pmset noidle
-- very nice when you have a desktop serving IPython notebook and a laptop connecting remotely.
caffeinate -i -s /bin/ssh ...
Explanation:
-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.
I find that for macOS 13, Ventura, sudo pmset sleep 0
will change the Settings > Displays > Advanced > Energy > Prevent automatic sleeping when the display is off, to enabled.
Similarly, setting it to anything other than 0
will disable that setting (and presumably change some value elsewhere).
You have to exit and restart settings to observe the change - it doesn't update as soon as the pmset command has run.
pmset -c sleep 0
.
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
use caffeinate.
the installed policies on my machine make everything dependent on the display, so the only thing that worked for me was to invoke caffeinate
with the -d
option.
$ caffeinate -d
excerpt from the man page:
CAFFEINATE(8)
NAME
caffeinate – prevent the system from sleeping on behalf of a utility
SYNOPSIS
caffeinate [-disu] [-t timeout] [-w pid] [utility arguments...]
DESCRIPTION
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.
Available options:
-d Create an assertion to prevent the display from sleeping.