Keep Claude Code and Codex Running When Your MacBook Lid Is Closed

Configure your MacBook to keep Claude Code, Codex, and AI agents running when lid is closed. Reversible script disables lid-close sleep.

lunes, 27 de julio de 2026 • 3 min read • Q2BSTUDIO Team

Cómo evitar que el MacBook duerma al cerrar la tapa

When you work with AI agents like Claude Code or Codex, productivity depends on those processes running without interruptions. But on a MacBook, closing the lid usually stops SSH sessions, background jobs, and of course the AI assistants themselves. If you are a developer or technical lead, you know that interrupting an agent mid‑task can mean lost time and context. Fortunately, there is a lightweight and reversible solution that lets your MacBook keep running with the display off but the system active, ideal for development and automation environments.

The approach involves modifying the operating system’s sleep behavior using native tools like pmset and a small shell script that monitors the lid state every two seconds. When the sensor detects the lid is closed, the script turns off the display but prevents the machine from sleeping. That way, Claude Code, Codex, SSH connections, or any automated task continue running without issues. This setup is intended for stationary machines, plugged in and well ventilated, not for carrying the laptop in a backpack while it is on.

In the world of software development and digital transformation, keeping processes uninterrupted is critical. Companies like Q2BSTUDIO, specialized in custom software, know that engineering team efficiency depends on tools that do not go down. That is why solutions like this script integrate perfectly into workflows where AI agents are used for code review, documentation generation, or data analysis. Moreover, by keeping the MacBook awake, you can leverage cloud services like cloud AWS/Azure without fear that a lid close will break remote connections.

From a technical perspective, the setup involves cloning a Git repository, running an installer, and then a LaunchAgent runs the monitoring script. The most interesting part is that the change is fully reversible: a simple uninstaller restores normal sleep behavior. For those working in cybersecurity or Business Intelligence, keeping active sessions is vital. A pentesting script or a Power BI ETL process may require hours of execution, and losing them due to an accidental lid closure delays deliveries. Q2BSTUDIO also offers cybersecurity and BI / Power BI services where operational reliability is key.

The script itself is minimalist: it checks AppleClamshellState every two seconds using ioreg; if the lid is closed, it runs pmset disablesleep 1 and turns off the display with pmset displaysleepnow. If the lid is opened, it restores normal mode. It is an elegant solution because it does not require third‑party software or compromise security. By integrating it with AI agents, you can have Claude Code or Codex working for hours unattended while you handle other tasks. This is especially useful for teams developing AI or automating processes with process automation.

Of course, there are important caveats. The MacBook must always be plugged in, because the battery would drain quickly with the CPU active and the display off. Also, ventilation is critical; when closed, heat dissipates less efficiently, so it is advisable to use a cooling pad or ensure airflow is not blocked. Also note that manual sleep will stop working while disablesleep is enabled. Finally, it is not recommended to carry the laptop in a bag while in this mode as it could overheat.

In conclusion, for developers using Claude Code, Codex, or other AI agents, this small shell configuration can make the difference between constantly restarting tasks or enjoying a continuous workflow. And if you need to go further, at Q2BSTUDIO we offer custom solutions, cloud integration, cybersecurity, and data analysis that boost these environments. Keeping the machine awake with the lid closed is just one example of how small technical tweaks can have a big impact on an engineering team’s productivity.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.