# Keep Remote Mac Sessions Alive Between Logins

Canonical: https://figment.so/blog/mac-remote-desktop-persistent-sessions

Run the job inside a named tmux session and hold a caffeinate assertion. The job then keeps running after the remote desktop connection drops. Any Figment plan includes SSH for re-attaching.

**Start the job inside a named `tmux` session and hold a `caffeinate` power assertion.** Then the job keeps running when your laptop closes or the connection drops, and you can re-attach later over SSH. The `tmux` behaviour below is from the [tmux(1) manual](https://man.openbsd.org/tmux), and LaunchAgent behaviour is from Apple's [Creating launchd Jobs](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html).

## Why do my processes die when I disconnect?

Two things usually kill a long job. First, a terminal opened by a remote desktop client is attached to a pseudo-terminal (pty). When the client closes, the pty closes, and the kernel sends `SIGHUP` to the foreground process group, which ends the process unless it ignores the signal. Second, macOS can idle into sleep when nothing holds a power assertion, which suspends the network stack and stalls background jobs. Fix both and builds, test runs and data jobs outlast every disconnect.

## How do I keep a job running with tmux?

Start a named session before the long job. `tmux` runs a server process that owns sessions independently of any client. Install it once if it is missing:

```bash
brew install tmux
```

```bash
tmux new-session -s build
```

Inside the session, run your build normally. To detach without killing it:

```
C-b d          # detach: session and its processes keep running
```

After reconnecting, re-attach:

```bash
tmux attach-session -t build
```

| Task | Command |
|---|---|
| New named session | `tmux new-session -s build` |
| Detach, keep running | `C-b d` |
| List sessions | `tmux ls` |
| Re-attach by name | `tmux attach-session -t build` |
| Re-attach to most recent | `tmux attach` |
| Kill a finished session | `tmux kill-session -t build` |
| Rename | `tmux rename-session -t old new` |
| New window inside session | `C-b c` |
| Split pane left/right | `C-b %` |
| Split pane top/bottom | `C-b "` |
| Peek without attaching | `tmux capture-pane -t build -p \| tail -20` |

Name sessions by job type plus a short date so `tmux ls` makes sense after a weekend away, for example `release-20261110`, `tests-integration` or `data-etl`.

## How do I stop the Mac from sleeping mid-job?

Use `caffeinate`. It ships with macOS and takes an IOKit power-management assertion, the same mechanism Xcode uses when it shows "Preventing Sleep". Wrap a single job, and the assertion is released when the command exits:

```bash
caffeinate -i make all 2>&1 | tee build.log
```

Or hold one assertion for several jobs:

```bash
caffeinate -i &
echo "caffeinate PID: $!"
```

`-i` prevents idle sleep while letting the display dim. Add `-d` only if something needs the display on. `-s` prevents system sleep but only on AC power, so it matters on Mac mini and Mac Studio hardware, not on laptops running on battery.

## How do I start caffeinate automatically?

Use a LaunchAgent. A LaunchAgent in `~/Library/LaunchAgents/` starts when your user session opens and restarts the program if it exits. Put this at `~/Library/LaunchAgents/com.figment.caffeinate.plist`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.figment.caffeinate</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/caffeinate</string>
        <string>-i</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>
```

Set permissions and load it without rebooting:

```bash
chmod 600 ~/Library/LaunchAgents/com.figment.caffeinate.plist
launchctl load  ~/Library/LaunchAgents/com.figment.caffeinate.plist
launchctl list | grep caffeinate     # a PID in the first column means it is running
```

To stop it:

```bash
launchctl unload ~/Library/LaunchAgents/com.figment.caffeinate.plist
```

`Label`, `ProgramArguments`, `RunAtLoad` and `KeepAlive` are the keys Apple documents for this pattern. `man launchd.plist` lists the others you may want, such as `StandardOutPath`, `WorkingDirectory` and `EnvironmentVariables`.

## Which system settings back this up?

Add a system-level backstop on a Mac you use as a build host:

1. Open **System Settings → Energy Saver** (desktop Macs) or **Battery** (portable Macs).
2. Set **Turn display off after** to `Never`.
3. Enable **Prevent automatic sleeping when the display is off** if it is shown.
4. On Mac mini or Mac Studio, enable **Start up automatically after a power failure** so the host comes back ready to resume LaunchAgents.

These settings persist across reboots and pair with the LaunchAgent above. Settings shown on a Figment VM can differ from a physical Mac, so check what your plan's System Settings offers.

## How do I check progress without staying connected?

Write the build output to a log inside the session, then detach:

```bash
# inside tmux
make all 2>&1 | tee ~/builds/release.log
# C-b d
```

Peek at the live pane without attaching:

```bash
tmux capture-pane -t build -p | tail -40
```

Or tail the log from any shell on the Mac, including an SSH session:

```bash
tail -f ~/builds/release.log
```

Every Figment plan includes SSH, so you can check progress without opening the remote desktop at all.

## Where should a long-running tmux session live?

Any plan can host a tmux session. Use the [Intel Mac VM](/mac-hosting#intel-vm) ($0.15 an hour, $25 a month) for scripts, data jobs and Xcode 26.3 or earlier. Use the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month) for arm64-only binaries, Xcode 26.4 or later and visionOS. Billing runs until you stop the Mac in Figment, even when you are disconnected or macOS is shut down, so stop an hourly Mac when a long job is finished.

## What else should I read?

- [Mac Remote Session Handover](/blog/mac-remote-session-handover)
- [Mac Remote Desktop File Transfer](/blog/mac-remote-desktop-file-transfer)
- [Mac GitHub Actions Self-Hosted Runner](/blog/mac-github-actions-self-hosted-runner)
