> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch02-getting-started/2.3-starting-the-gateway-daemon.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 2.3 Starting the Gateway Daemon

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~140,000 tokens, Output \~10,000 tokens (total for this chapter) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

The Gateway is the core process of OpenClaw — it must run continuously to receive and process messages from various channels. This section explains how to start the Gateway and how OpenClaw implements daemon management.

## 2.3.1 Foreground Startup vs Daemon Mode

### Foreground Startup

The simplest way to start is to run in the terminal foreground:

```bash
# Start Gateway in foreground
openclaw gateway --port 18789 --verbose

# Or run from source
pnpm openclaw gateway --port 18789 --verbose
```

In foreground mode, the Gateway runs directly in the current terminal with logs output to stdout. Closing the terminal or pressing Ctrl+C will stop the Gateway. This mode is suitable for development and debugging.

### Daemon Mode

In production environments, you want the Gateway to run continuously in the background and automatically start after a system reboot. OpenClaw achieves this using native system service management tools:

* **macOS**: launchd (`launchctl`)
* **Linux**: systemd (`systemctl`)
* **Windows**: Task Scheduler (via `schtasks`)

Install the daemon:

```bash
openclaw onboard --install-daemon
# Or install separately
openclaw daemon install
```

## 2.3.2 Source Code Analysis of launchd / systemd Service Installation

The daemon management source code is located in the `src/daemon/` directory (30 files), divided into several modules by operating system platform:

```
src/daemon/
├── service.ts             # Top-level service management interface
├── launchd.ts             # macOS launchd management
├── launchd-plist.ts       # launchd plist file generation
├── systemd.ts             # Linux systemd management
├── systemd-unit.ts        # systemd unit file generation
├── systemd-linger.ts      # systemd linger configuration
├── systemd-hints.ts       # systemd usage hints
├── systemd-availability.ts # systemd availability detection
├── schtasks.ts            # Windows Task Scheduler management
├── node-service.ts        # Node.js service common logic
├── paths.ts               # Service file path management
├── constants.ts           # Constant definitions
├── diagnostics.ts         # Diagnostic tools
├── inspect.ts             # Service inspection
├── program-args.ts        # Startup argument construction
├── runtime-parse.ts       # Runtime parsing
├── runtime-paths.ts       # Runtime paths
├── service-audit.ts       # Service audit
├── service-env.ts         # Service environment variables
└── service-runtime.ts     # Service runtime configuration
```

### macOS: launchd Service

On macOS, OpenClaw generates a launchd plist file and installs it as a user-level service. `src/daemon/launchd-plist.ts` is responsible for generating this XML configuration:

```xml
<!-- Generated plist structure illustration -->
<?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>ai.openclaw.gateway</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/node</string>
        <string>/path/to/openclaw.mjs</string>
        <string>gateway</string>
        <string>--port</string>
        <string>18789</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>~/.openclaw/logs/gateway.stdout.log</string>
    <key>StandardErrorPath</key>
    <string>~/.openclaw/logs/gateway.stderr.log</string>
</dict>
</plist>
```

Key configurations:

* **`RunAtLoad`**: Automatically run when the system starts
* **`KeepAlive`**: Automatically restart if the process crashes

`src/daemon/launchd.ts` encapsulates calls to the `launchctl` command (`load`, `unload`, `list`, etc.), providing operations like install, uninstall, and restart.

### Linux: systemd User Service

On Linux, OpenClaw generates a systemd user unit file. `src/daemon/systemd-unit.ts` is responsible for generating:

```ini
# Generated unit file structure illustration
[Unit]
Description=OpenClaw Gateway
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/node /path/to/openclaw.mjs gateway --port 18789
Restart=always
RestartSec=5

[Install]
WantedBy=default.target
```

Since it's a user-level service (`~/.config/systemd/user/`), no root privileges are needed to install and manage it. `src/daemon/systemd-linger.ts` handles the `loginctl enable-linger` configuration to ensure the user service runs even when the user is not logged in.

### Windows: Task Scheduler

On Windows (requires WSL2), `src/daemon/schtasks.ts` uses `schtasks.exe` to create scheduled tasks. Since OpenClaw strongly recommends running in WSL2, Windows native Task Scheduler is primarily used to automatically start the Gateway in WSL when the system boots.

### Unified Service Management Interface

`src/daemon/service.ts` provides a cross-platform unified interface:

```typescript
// Conceptual illustration (simplified)
interface DaemonService {
  install(options: InstallOptions): Promise<void>;
  uninstall(): Promise<void>;
  start(): Promise<void>;
  stop(): Promise<void>;
  restart(): Promise<void>;
  status(): Promise<ServiceStatus>;
}
```

Regardless of whether the underlying implementation uses launchd, systemd, or schtasks, the upper-level code (CLI commands `openclaw daemon install/start/stop`) operates through this unified interface.

## Daemon Lifecycle Management

```bash
# Install and start the daemon
openclaw daemon install

# Check status
openclaw daemon status

# Restart
openclaw daemon restart

# Stop
openclaw daemon stop

# Uninstall
openclaw daemon uninstall
```

After installation, the Gateway will continue running in the background. You can verify it as follows:

```bash
# Check health status
openclaw doctor

# Test by sending a message via CLI
openclaw agent --message "Hello" --thinking high
```

`openclaw doctor` is a comprehensive diagnostic command that checks:

* Whether the Gateway process is running
* Whether the port is reachable
* Whether the configuration file is valid
* Channel connection status
* Model authentication status
* Whether security configuration is reasonable

When everything is working correctly, your personal AI assistant is running in the background. Next, let's see how to develop efficiently.
