Home/Guides/How to keep a remote Mac mini awake during a task with caffeinate

How to keep a remote Mac mini awake during a task with caffeinate

Updated · airankrush

What this guide covers

When you finish, your long tasks no longer pause because of system idle sleep, and everything returns to its original state once the task ends. You need: basic terminal skills.

Steps

  1. First tell three things apart: disconnecting, display off, and sleep

    Disconnecting the remote desktop only ends the remote access session; the remote system keeps running as usual. The display turning off does not affect background computation either. What really pauses a task is system sleep. So that is the only one you need to guard against, and only while the task is running. Closing the lid of your own laptop has nothing to do with the remote machine; see whether tasks stop when the lid is closed.

  2. Wrap the command in caffeinate

    The most common form is caffeinate -i <your command>, for example caffeinate -i ./train.sh. According to the system manual, -i creates an assertion that prevents system idle sleep. The assertion is valid while that command runs and is released automatically as soon as the command ends. It writes nothing to system settings, so it does not conflict with the power configuration the platform delivered, and it needs no administrator password.

  3. Choose flags as needed

    The options in the manual: -i prevents system idle sleep. -s prevents system sleep and is valid only on AC power; a Mac mini is always on mains power, so it can be used. -d prevents display sleep, and should be added only when the task really needs the screen to stay on. -m prevents the disk from idle sleeping. They can be combined, for example caffeinate -is. For unattended computation, -i is generally enough.

  4. Add it to a process that is already running

    If the task has already started and you forgot to add it, there is no need to start over. Find the process ID with pgrep -f <keyword>, then run caffeinate -i -w <PID>: it waits until that process exits and then ends by itself. Put this command in another tmux window so it does not die with the terminal window.

  5. With no specific command, hold for a set time

    caffeinate -i -t <seconds> keeps the machine awake for the given number of seconds and ends automatically when time is up. It suits waiting for a process you did not start yourself, such as an export in a graphical app. Note what the manual says: -t has no effect when a command follows it, in which case the command's running time applies. Running caffeinate with no arguments at all holds until you press Ctrl+C.

  6. Confirm it is working, read-only

    pmset -g assertions lists every current sleep-prevention assertion and who created it. If you can see your caffeinate process, it is working. pmset -g shows the current power settings. Both commands only display information. Changing power settings falls within the platform's delivered configuration: contact support first if something needs adjusting, do not follow online tutorials to change them, and do not install always-on utilities that rewrite system power settings.

  7. What it cannot help with

    caffeinate only prevents sleep. It cannot stop the program crashing by itself, the terminal window being closed, or the machine being restarted for a system update or maintenance. Those are handled by logs, tmux and start-at-login jobs respectively. Also, do not shut the machine down yourself: after a shutdown someone may have to power it on in person, and the remote connection drops with it. See whether you can shut down or sleep the machine.

FAQ

Can I just set the system to never sleep?

Do not change it yourself. Power-related settings are part of the delivered configuration, and whether the machine currently sleeps at all is determined by the state it was delivered in. Look with pmset -g first, and check with support if you need an adjustment.

Are tasks still running after the screen locks?

A locked screen only means the password has to be entered again to use the desktop. Background processes keep running. When you come back, unlock it by entering the Mac account password through the remote desktop.

Should scheduled jobs use caffeinate too?

For long-running ones, we suggest it: put caffeinate -i in front of the job's command. The system manual says a launchd calendar job missed during sleep runs on wake, but a job already running that gets interrupted by sleep can only be protected this way.

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All guides