跪拜 Guibai
← All articles
Frontend · Backend · Developer

bm2: A 2.8 MB pm2 Replacement Written in MoonBit

By 前端之虎陈随易 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Most pm2 deployments use only a few commands but carry the full weight of a Node.js runtime and dozens of unused features. A 2.8 MB native binary that idles at 2.7 MB of RAM makes process management viable on low-resource VPS instances and edge devices where Node-based tooling is too heavy.

Summary

bm2 strips pm2 down to the handful of commands most developers actually use: start, stop, status, and auto-restart. A single TOML config file defines apps, instance counts, and base ports. The tool ships as two files and runs a background service that stays under 3 MB of resident memory with 3 ms command response times.

MoonBit compiles to a compact native binary with no runtime, which directly produces the 2.8 MB binary size and low memory overhead. The language's concise syntax also makes the codebase easy to maintain and extend.

Multi-instance execution with automatic port assignment, memory-limit protection, and a confirmation prompt before stopping all apps are built in. Background service upgrades happen without restarting managed applications.

Takeaways
bm2 is a single native binary (2.8 MB) that manages Node.js and Bun.js processes on Linux.
Configuration uses a TOML file specifying app name, working directory, entry script, instance count, and base port.
The background service consumes 2.7 MB of resident memory and responds to commands in 3 ms.
Automatic restart revives crashed processes without manual intervention.
Memory-limit protection kills runaway processes before they exhaust system resources.
Multi-instance mode assigns ports sequentially from a base port.
Stopping all apps triggers a confirmation prompt to prevent accidental shutdowns.
Background service upgrades happen without downtime for managed applications.
Conclusions

MoonBit's native compilation eliminates the Node.js runtime dependency that makes pm2 itself a non-trivial resource consumer, which is ironic for a process manager meant to be lightweight.

The two-file distribution model (binary plus config) sidesteps npm entirely, which matters on minimal Linux images where a Node toolchain isn't installed.

Porting a well-understood tool like pm2 into a newer language is a practical way to stress-test that language's standard library and FFI for system-level work.

Concepts & terms
MoonBit
A relatively new programming language that compiles directly to native machine code with no runtime, producing compact binaries with low memory overhead. Syntax is concise and inspired by modern languages like Rust and Go.
pm2
A widely used process manager for Node.js applications that handles daemonization, monitoring, auto-restart, and log management. It runs on Node.js itself and offers dozens of commands.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗