--- title: "zhin start" description: "Start the management gateway and the Zhin instance: development, production, background and CI smoke runs; stop / restart / status / logs, the local control socket, exit codes and systemd" --- # zhin start `zhin start` starts the project's **management gateway** (ADR 0054): a long-lived process that listens on `http.port`, serves the Console and launches the **Zhin instance** (IM Runtime, plugins, adapters, Agent) as a child process. While the instance crashes, is stopped or installs dependencies, the gateway keeps running and the Console still shows its state and logs. Your TypeScript source runs directly with no build step; watching is enabled only by an explicit `--watch`. `zhin runtime start` is an alias of `zhin start --foreground` (gateway in the foreground). ## Common usage ```bash # Daily development: gateway and instance in the foreground, HMR + Console; Ctrl+C stops both pnpm dev # = zhin start --foreground --watch source config # Server: run in the background (the default); logs go to .zhin/logs/ zhin start --mode production --no-watch zhin status # gateway and instance state (--json for scripts) zhin logs -f # instance logs zhin restart # restart the instance only; gateway and Console stay up zhin stop # stop the instance and the gateway # systemd / Docker / PM2: the service manager owns a foreground process zhin start --foreground --mode production --no-watch # CI smoke test: start once to verify configuration and assembly; the gateway exits with it zhin start --once --mode test ``` | Situation | Use | | --- | --- | | Local development | `pnpm dev`: foreground, watches files, hot reload | | A server without a process manager | `zhin start`: background gateway, managed with `zhin status / logs / restart / stop` | | A server managed by systemd / Docker / PM2 | `zhin start --foreground --mode production --no-watch`: foreground, logs to stdout | | Checking config and assembly in CI | `--once`: start once, print JSON and exit | ## Options | Option | Description | | --- | --- | | `-f, --foreground` | Run the gateway in the foreground (development, systemd, containers, PM2) and echo the instance output. When stdin is a terminal and the gateway is the terminal's foreground job, the instance shares that input (terminal adapters, login assist); a gateway started as a background job (`… &`) keeps it from the instance, also after a later `fg`. When the instance exits or crashes, the gateway restores the terminal settings it started with (`stty`; not on Windows). `--once` and `--standalone` always run in the foreground | | `-d, --daemon` | Run in the background (what `zhin start` does by default). The background gateway's own output goes to `.zhin/logs/gateway.log` | | `--mode ` | Runtime mode: `development` (default) / `test` / `production` | | `--environment ` | Environment name, defaults to `--mode`; selects `.env.` and must match `/^[a-z0-9][a-z0-9-]*$/` | | `--watch ` | Explicitly watch `source` / `config`; combine as `--watch source config`. Without it nothing is watched | | `--no-watch` | Compatibility flag to watch nothing; cannot be combined with `--watch` | | `--once` | Start once, print a JSON snapshot and exit (the gateway exits with the instance) | | `--open` | Open the Remote Console in a browser after a TTY start; `ZHIN_OPEN=1` does the same | | `--log-file ` | Instance log file, default `/.zhin/logs/instance.log` (size-rotated; read by `zhin logs`) | | `--standalone` | Development / test only: no gateway, the instance runs its own Node HTTP server in this process; no managed restarts | `zhin start --help` lists these options with examples. Unknown options fail fast before startup instead of entering a restart loop. ## Management commands | Command | Gateway running | Gateway not running | | --- | --- | --- | | `zhin start` | Says it is already running and shows the status | Starts gateway and instance in the background, waits for the control socket, prints the HTTP address and Console link | | `zhin stop [--timeout ]` | Sends `gateway.shutdown`: stops the instance, then the gateway, and waits for the process to exit | Nothing to do (a pre-gateway process with only `.zhin.pid` is still stopped) | | `zhin restart` | `gateway.instance.restart`: restarts the instance only | Nothing to do | | `zhin restart --gateway` | Stops the background gateway and starts it again with its original options; a foreground gateway (`--foreground`) cannot be restarted in place, see below | Nothing to do | | `zhin status [--json]` | Gateway pid, mode, version, HTTP addresses, Console link; instance state (`stopped / starting / running / stopping / backoff / crashed`, pid, generation, last failure and log tail); while the instance runs, whether each adapter account is online, the MCP server states and the number of attention items (see below) | Says it is not running, **exit code 3** | | `zhin logs [-f] [-n N]` | `gateway.instance.logs`: the last N lines (1–1000, default 200); `-f` polls for new lines; with `--level` / `--since` / `--until` / `--grep` / `--name` it searches the log files (see the [CLI reference](./index.md)) | Reads `.zhin/logs/instance.log`; `-f` follows the file | | `zhin install / uninstall / update` | Runs the dependency transaction through the gateway: stop instance → package manager → start instance, rolled back on failure; progress is streamed | Runs the same transaction directly in the project | A foreground gateway (`zhin start --foreground`, the recommended way under systemd, PM2, launchd and containers) is attached to a terminal or a service manager, so `zhin restart --gateway` does not stop it: it explains why, exits with code `1` and prints the replacement command. On Linux it reads the gateway process's environment to identify the service manager: systemd (`INVOCATION_ID`, unit name from the process's cgroup) gets `sudo systemctl restart ` (`systemctl --user restart ` for a user service), PM2 (`pm_id`) gets `pm2 restart `; on macOS it finds the launchd job running the gateway process in `launchctl list` (`zhin service install` uses the label `com.zhinjs.`, the process's `XPC_SERVICE_NAME`) and prints `launchctl kickstart -k gui//