# Restore a Hosted Mac Agent After a Host Reboot

Canonical: https://figment.so/blog/mac-agent-reboot-recovery

Run the agent as a launchd user agent with RunAtLoad and KeepAlive, turn on automatic login, then check four things after a reboot. Any Figment Mac plan works, and the Intel VM ($25 a month) suits script agents.

To have a Mac agent come back after a reboot, run it as a launchd user agent with `RunAtLoad`, set `KeepAlive` to match how it exits, and turn on automatic login for the agent's user account. Then confirm four things after every reboot: the plist loaded, the log has a new line, credentials work without a prompt, and a canary task completes. Reboots follow OS updates and other restarts, so plan for them. Any Figment Mac plan can run this, and the Intel Mac VM ($25 a month) fits Python, Node, bash and Shortcuts agents.

## Which Figment plan fits an agent workload?

Any plan can run a launchd user agent, because the mechanism is built into macOS. The plan decides what the agent can do after it restarts, not whether it restarts. Prices are USD before tax, with details on the [Mac hosting page](/mac-hosting).

| Plan | Price | Resources | Best fit |
|---|---|---|---|
| [Intel Mac VM](/mac-hosting#intel-vm) | $25 a month, $2 a day, $0.15 an hour | 4 shared vCPUs, 4GB RAM, 80GB SSD, macOS Sequoia | API-call agents, git operations, file processing, Shortcuts orchestration |
| [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) | $39 a month, $6 a day, $0.40 an hour | 4 shared vCPUs, 4GB RAM, 65GB SSD | Agents that run arm64-only binaries or Xcode 26.4 or later |
| [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) | $349 a month ($299 on a 12-month term) | Whole Mac: M4 Pro, 48GB RAM, 512GB SSD | Heavy or isolated agent work |

A Python, Node, Shortcuts or bash polling agent behaves the same on the Intel VM as on any Mac. Apple lists Xcode up to 26.3 for macOS Sequoia, and the Intel VM cannot run arm64-only tools (see Apple's [porting guide](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon)). Billing continues through a reboot or a shutdown; stop an hourly or daily Mac in Figment to end it.

For an unattended agent, enable automatic login for the agent's user account in **System Settings, Users & Groups, Automatic login**, so launchd user agents load after any reboot without someone logging in.

## Which launchd restart pattern should I use?

Use the pattern that matches how your agent exits. The [Apple launchd guide](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) and the [launchd.plist(5) manual](https://keith.github.io/xcode-man-pages/launchd.plist.5.html) describe them.

| Pattern | Behavior | Use when |
|---|---|---|
| `KeepAlive: true` | Restart after every exit, clean or not | Long-running process that must never stop |
| `KeepAlive` dict with `SuccessfulExit: false` | Restart only after a non-zero (crash) exit | Polling loop that exits 0 when the queue is empty |
| `KeepAlive` dict with `SuccessfulExit: true` | Restart only after a clean exit | Job you want to repeat continuously |
| `RunAtLoad: true` and no `KeepAlive` | Run once at load, do not restart | One-shot initialization task |

A polling agent that exits 0 when the queue drains should use `SuccessfulExit: false`. launchd restarts it on a crash, and `StartInterval` schedules the next poll.

```xml
<!-- ~/Library/LaunchAgents/com.yourteam.agent.plist -->
<?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.yourteam.agent</string>

  <key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>/Users/agent/agent-workspace/agent-loop.sh</string>
  </array>

  <!-- Poll every 90 seconds even after a clean exit -->
  <key>StartInterval</key>
  <integer>90</integer>

  <!-- Restart on crash; leave alone on clean exit -->
  <key>KeepAlive</key>
  <dict>
    <key>SuccessfulExit</key>
    <false/>
  </dict>

  <!-- Log both streams for post-reboot diagnosis -->
  <key>StandardOutPath</key>
  <string>/Users/agent/agent-workspace/agent.log</string>
  <key>StandardErrorPath</key>
  <string>/Users/agent/agent-workspace/agent-err.log</string>

  <!-- Run once immediately when loaded (and after each login) -->
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
```

Apple's guide warns that a job which exits within 10 seconds of launch may be treated as crashing and throttled. By default launchd will not spawn it more than once every 10 seconds. Have the agent script spend at least that long on startup, for example on a short health check (DNS, API reachability, Keychain access).

## How do I confirm recovery after a reboot?

Run four phases in order, each with one acceptance check.

### Phase 1: Did the plist load?

```bash
# Is the agent known to launchd?
launchctl list | grep com.yourteam.agent
```

Expected output is a row with the label and either `0` (not currently running) or a PID. If the label is missing, reload the plist:

```bash
launchctl load ~/Library/LaunchAgents/com.yourteam.agent.plist
```

**Acceptance check 1:** the label appears in `launchctl list`.

### Phase 2: Did the agent run after the reboot?

```bash
# Compare the last boot time to the newest log line
last reboot | head -1
tail -20 ~/agent-workspace/agent.log
```

The log should show at least one entry timestamped after the reboot. If it is empty or stale, read `agent-err.log` for a startup error.

**Acceptance check 2:** at least one log line timestamped after the reboot.

### Phase 3: Can the agent read its credentials without a prompt?

Keychain items survive a reboot, but the first retrieval can prompt if the Keychain is set to lock. Set the login Keychain to not auto-lock on timeout or sleep:

```bash
# Disable the timeout and sleep auto-lock on the login keychain
security set-keychain-settings ~/Library/Keychains/login.keychain-db
```

Then confirm the agent can retrieve its secret without a GUI prompt:

```bash
security find-generic-password -a "$USER" -s "llm-api-key" -w >/dev/null && echo OK
```

If this triggers an interactive Keychain prompt, the agent will block unattended after a reboot. Keep the secret in a Keychain the agent user can open without a prompt, or source it from the CI secret manager at job start.

**Acceptance check 3:** `security find-generic-password` returns exit code 0 with no GUI prompt.

### Phase 4: Does a canary task finish?

Push one known-good task through your queue and confirm it is processed within two poll intervals:

```bash
# After enqueuing the canary, wait 3 minutes then inspect the agent's output
sleep 180
git -C ~/agent-workspace log --oneline -3
```

**Acceptance check 4:** the canary commit (or equivalent downstream artifact) appears.

## What do I check when a phase fails?

Start with this list:

- **Label missing from `launchctl list`:** the plist is not in `~/Library/LaunchAgents/` or is malformed. Validate it with `plutil -lint ~/Library/LaunchAgents/com.yourteam.agent.plist`.
- **Log empty after reboot:** automatic login is not configured, or the script path moved. Check with `ls -la /Users/agent/agent-workspace/agent-loop.sh`.
- **Keychain prompts interactively:** run `security set-keychain-settings` without a timeout, and make sure the item's access list includes the agent binary.
- **Canary not processed:** the API key expired, a deploy key rotated, or the remote endpoint moved. The first line of `agent-err.log` normally says which.
- **Agent throttled by launchd:** the script exits in under 10 seconds. Add a startup health check that takes longer.

## How do I know a reboot went wrong without checking by hand?

Watch the log from a second machine or a monitoring service, and alert when no new line has been written for more than twice your `StartInterval`. That catches both a reboot launchd did not recover from and a running but stalled agent.

```bash
# On a monitoring host — alert if agent.log has not been modified in 10 minutes
find ~/agent-workspace/agent.log -mmin +10 -exec \
  curl -s -X POST "$ALERT_WEBHOOK" -d '{"text":"Agent log stale"}' \;
```

## What does it cost to keep an agent online?

On the Intel Mac VM, a month is $25, and a day is $2 if you only need the agent for a short job. Monthly plans renew on the payment date until you cancel, and access runs to the end of the paid period. Start on the Intel VM when the agent only runs Python, Node, bash or Shortcuts, and move to an Apple silicon plan only when the workload needs arm64-only binaries or a newer Xcode. Figment fixes infrastructure faults on the Mac, network and virtualization layer. Re-running these acceptance checks after a macOS update is your job. See the [Mac hosting plans](/mac-hosting) to choose.

Related: [Mac agent API polling](/blog/mac-agent-api-polling) · [Mac agent large uploads](/blog/mac-agent-large-uploads) · [GitHub Actions self-hosted runner on Mac](/blog/mac-github-actions-self-hosted-runner)
