Turn Your Old MacBook into a 24/7 AI Agent Server
If you run AI coding agents — Claude Code, Codex, OpenClaw, a Telegram bot of your own — you have felt the pain: a long session dies the moment you close the laptop lid, and the Mac goes to sleep mid-task. The usual fix is a VPS, but a VPS with enough RAM runs $20–40 a month, adds latency to every tool call, and holds your API keys on someone else's disk.
There is probably a better server already in your drawer. A spare MacBook — even one with a broken screen — has more CPU and RAM than a small cloud instance, runs your macOS tooling natively, is silent, and its battery doubles as a built-in UPS. This guide sets one up as a 24/7 agent server. You can do it the manual way, but we'll use MacHead, a free open-source app that replaces the fiddly parts (clamshell sleep, dummy display plugs, keep-awake hacks) with one click.
What you need
- A MacBook you don't use as your daily driver — any Apple Silicon or Intel model on macOS 12+, broken screen welcome
- Its power adapter (24/7 duty means staying plugged in)
- Network: Wi-Fi works; a USB-to-Ethernet adapter is more reliable
- For a broken-screen machine: an external monitor for the one-time setup, or another Mac for Screen Sharing
- Your agent of choice installed (Claude Code, Codex, etc.)
Why a closed MacBook sleeps — and why caffeinate isn't enough
macOS has two different sleeps. Idle sleep is what caffeinate and keep-awake apps block. Clamshell sleep fires when you close the lid, at a lower level, and it pauses everything — agent, network, tunnels — within seconds. A keep-awake assertion does not stop it.
The manual workarounds are a dummy HDMI plug (so macOS thinks a monitor is attached) or sudo pmset -a disablesleep 1 — a global, sticky switch with no battery protection that you must remember to turn off. Both work; both are exactly the kind of thing that bites you at 3 a.m. when the battery dies or an update resets your settings.
The manual setup (what most guides tell you)
- Set sleep to never in System Settings, then fight clamshell sleep separately with pmset or a dummy plug
- Install a keep-awake app for idle sleep on top of that
- Enable SSH and a remote desktop tool (RustDesk, Screen Sharing) for GUI prompts
- Write launchd plists so your agent restarts on crash and on boot
- Set up a tunnel or Tailscale for access away from home
- Remember to revert the pmset changes before travelling with the laptop again
None of this is hard, exactly — it's just six separate things to babysit forever. That is the gap MacHead fills.
The MacHead setup (about 10 minutes)
1. Install
Download MacHead (free, Apache-2.0), open the DMG and drag it into Applications. Launch it and grant the permissions it asks for. A broken screen is fine from here on — everything works headless.
2. Turn on headless mode
One click in the menu bar: the internal display turns off (no more glowing, heat-wasting panel), clamshell sleep is handled, the built-in keyboard and trackpad lock so nothing brushes against them, and the microphone auto-mutes — a machine that listens 24/7 shouldn't be an open mic.
3. Turn on the guards
Enable the battery guard (if power is lost and charge drops below a safe threshold, normal sleep resumes so the battery isn't deep-drained) and temperature alerts. Put the MacBook on a hard, ventilated surface — a closed lid reduces airflow, so not on a bed or in a bag.
4. Make it reachable
Open the web console (MacHead generates a random admin password on first launch) to watch CPU / memory / battery from your phone. For access away from home, enable one of the built-in tunnels — Cloudflare Tunnel, Microsoft Dev Tunnels or FRP — no port forwarding, nothing exposed raw to the internet. Monitoring hooks (ServerStatus, Uptime Kuma) are there if you already run them.
5. Start your agent, close the lid
Install your agent as usual, start it (tmux if you like), and close the lid. It keeps running. Check in from your phone over the tunnel, approve what needs approving, and let it work overnight.
MacBook vs VPS vs Raspberry Pi for agents
| Spare MacBook | VPS | Raspberry Pi | |
|---|---|---|---|
| Cost | $0/month (already yours) | $20–40/month for usable RAM | ~$50–80 once |
| Performance | Apple Silicon CPU, 8–32GB unified memory | Shared vCPU, plan-dependent | Slow; 4–8GB |
| Local models & tooling | Native macOS, Metal acceleration | Linux, usually no GPU | Linux ARM, limited |
| Power outage | Battery = built-in UPS | Datacenter UPS | Dies without a UPS |
| Your keys & code | Stay in your house | On a third-party disk | Stay in your house |
| Best when | You own a spare Mac | You need a public static IP | Power draw is the top priority |
Running notes from experience
- Plug in, always. Battery-only lid-closed running works, but for overnight agent runs mains power is the safe choice.
- Auto-start on login means the machine comes back by itself after updates and reboots — enable it once.
- It's still a laptop. Turn headless mode off and the internal display, keyboard and normal sleep all come back. Nothing to uninstall.
FAQ
Can I run an AI agent on a MacBook with a broken screen?
Yes. A broken screen doesn't affect the rest of the machine. Headless mode turns the internal display off and uses a virtual display, so the cracked panel is never needed. Do the one-time setup over Screen Sharing or with a temporary external monitor.
Why does my Mac still sleep with the lid closed even after pmset or caffeinate?
Caffeinate blocks idle sleep; lid closing triggers clamshell sleep at a lower level. The manual levers are a dummy HDMI plug or pmset -a disablesleep, a global sticky setting with no battery protection. MacHead wraps lid-closed operation with battery and thermal safeguards instead.
Is it safe to run a MacBook with the lid closed 24/7?
Yes, with two precautions: keep it plugged in on a hard, ventilated surface, and use a battery guard that restores normal sleep at low charge. MacHead includes the guard and temperature alerts.
Do I need Ethernet, or is Wi-Fi OK?
Wi-Fi works, but wired is more reliable for a machine that must stay reachable. On USB-C MacBooks you'll need an adapter. A tunnel like Cloudflare Tunnel makes it reachable from anywhere without port forwarding.
How much RAM do I need?
16GB comfortably handles agents plus memory search and parallel sub-agents. 8GB is fine for cloud-API agents like Claude Code, since the model runs remotely — local RAM matters most for local models and tooling.
MacBook, VPS or Pi — which should I pick?
A VPS if you need a public static IP; a Pi if power draw rules everything; a spare MacBook if you already own one — it's the strongest agent server that costs $0 a month.
Get started: MacHead is free and open source (Apache-2.0), for Apple Silicon & Intel on macOS 12+.
Download MacHead GitHub What is MacHead?