A lightweight Linux process manager for Bun and Node.js applications, written in MoonBit
| 场景 | 说明 |
|---|---|
| 一台服务器放多个项目 | 常驻开销小,装上之后基本不用维护 |
| 接口需要多实例扛并发 | 多个实例共用端口,网关侧只写一条 upstream |
| 夜间故障需要有人知道 | 崩溃、恢复、阈值越线都推到群里或邮箱 |
| 每日运行情况要留档 | 每天一份统计,逐实例列出状态与资源占用 |
| 换进程管理器不想停业务 | 升级 bm2 本身不重启应用 |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
moon install chensuiyi/bm2/...{
"registry": "https://mooncakes.io/",
"download": "https://mirrors.ustc.edu.cn/mooncakes",
"index": "https://mooncakes.io/git/index",
"api": "https://mooncakes.io",
"symbols": "https://mirrors.ustc.edu.cn/mooncakes/symbols.zip"
}name = "api"
script = "src/index.ts"
instances = 2
port = 3000bm2 start # 登记、拉起实例、写状态,成功时无输出
bm2 list # 看实例表const port = Number(process.env.BM2_APP_PORT)
Bun.serve({ port, reusePort: true, fetch: () => new Response("ok") })bm2 start # 读当前目录的 bm2.toml
bm2 start api/bm2.toml # 指定配置文件
bm2 start api # 指定目录,找其中的 bm2.tomlbm2 restart apibm2 list
bm2 list api
bm2 list --json│ 名称 │ 实例 │ PID │ 端口 │ 执行 │ 状态 │ 次数 │ 内存 │ 运行时长 │
│ bench │ 0 │ 108176 │ 4601 │ 独立 │ online │ 1 │ 26 MB │ 1h 12m │bm2 stop
bm2 stop apibm2 delete api # 日志留着,随时能翻
bm2 delete api -f # 连状态目录与日志一起删bm2 clean --dry-run # 先看会删哪些
bm2 cleanbm2 check配置正常:项目 "bench",1 个独立实例,端口 4601,运行时 bun
通知:未启用
端口 4601~4601:空闲bm2 reloadbm2 upgradebm2 notifyproject api has no notifications enabled: set [notify].enabled = true and add at least one [[notify.target]] in bm2.tomlbm2 kill -ybm2 versionbm2 0.10.0name = "api" # 项目名,也是状态目录名
script = "index.js" # 入口文件,相对 cwd
instances = 4 # 实例数
port = 3020 # 起始端口| 配置键 | 默认 | 什么时候写 |
|---|---|---|
| cwd | 配置文件所在目录 | 应用在别处,必须是绝对路径 |
| runtime | bun | 用 Node 就写 "node" |
| exec_mode | cluster | 要独立端口写 "fork" |
| max_memory_mb | 512 | 调整内存上限与告警线 |
| max_tree_processes | 512 | 应用会 fork 出很多子进程时调大 |
| max_restarts | 10 | 调整自愈次数,0 是首次异常即停 |
| restart_delay_ms | 1000 | 调整重启间隔 |
| min_uptime_ms | 10000 | 活够这么久才算真的起来了 |
| stop_timeout_ms | 10000 | 停机时给应用的优雅退出时间 |
| log_max_size_mb | 10 | 单个日志文件的上限 |
| log_keep_files | 10 | 保留几代历史 |
| lang | 跟随操作者 | 钉死这个项目的输出与通知语言 |
| [notify] | 关闭 | 开告警与每日统计 |
bm2: InvalidField: root has unknown field max_size_mbname = "api" # 项目名,也是状态目录名
script = "index.js" # 入口文件,相对 cwd
instances = 4 # 实例数
port = 3020 # 起始端口
cwd = "/wwwroot/api" # 默认是配置目录,应用在别处才写
runtime = "bun" # 用 Node 写 "node"
exec_mode = "cluster" # 要独立端口写 "fork"
max_memory_mb = 768 # 单实例内存上限,也是告警基准
max_restarts = 10 # 自愈次数,0 是首次异常即停
restart_delay_ms = 1000 # 异常退出后等多久再拉起
min_uptime_ms = 30000 # 活过这么久才算真的起来了
stop_timeout_ms = 15000 # 停机的优雅退出时间
lang = "zh" # 输出与通知语言
log_max_size_mb = 10 # 单个日志文件上限
log_keep_files = 10 # 保留几代历史
[notify]
enabled = true
mail_url = "smtps://smtp.example.com:465" # 用邮箱才写
mail_from = "bm2@example.com"
[[notify.target]]
platform = "feishu" # 也可填 email
url = "__FEISHU_WEBHOOK__"
events = ["!lifecycle"] # 不写就收全部事件| 对比项 | cluster | fork |
|---|---|---|
| 端口 | 所有实例共用 port | 从 port 起连续排开 |
| 分发者 | 内核 SO_REUSEPORT | 网关,或没人分发 |
| 应用要求 | 多实例时须开 reusePort | 无 |
| 网关写法 | 一条 upstream | 每个实例一条 upstream |
| 改实例数 | 不用动网关 | 要同步 upstream 并 reload |
| 请求落点 | 新连接按内核规则分配 | 由网关决定,可按权重与健康状态 |
upstream api {
server 127.0.0.1:3000;
}
server {
server_name api.example.com;
location / {
proxy_pass http://api;
}
}upstream api {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
server_name api.example.com;
location / {
proxy_pass http://api;
}
}# cluster
api.example.com {
reverse_proxy 127.0.0.1:3000
}
# fork
api.example.com {
reverse_proxy 127.0.0.1:3000 127.0.0.1:3001 127.0.0.1:3002
}| 顺序 | 文件 | 用途 |
|---|---|---|
| 1 | .env | 通用 |
| 2 | .env.production | 生产覆盖 |
| 3 | .env.local | 本机覆盖 |
| 4 | .env.production.local | 本机覆盖且生产 |
InvalidDocument: /srv/api/.env line 4: expected '=' after DATABASE_URL| 变量 | 值 |
|---|---|
| BM2_APP_NAME | 项目名 |
| BM2_INSTANCE_ID | 实例编号,第一个是 "0" |
| BM2_APP_INSTANCE | 同实例编号,命名对齐 pm2 的 NODE_APP_INSTANCE |
| BM2_APP_PORT | 分配给这个实例的端口 |
| NODE_ENV | serve 下是 production,dev 下是 development |
// 读到这里,就不用关心自己被第几个拉起
const port = Number(process.env.BM2_APP_PORT)~/.bm2/projects/<项目名>/logs/
├── api.log 当前普通日志
├── api.1.log 上一代,依次 .2 .3
├── api.error.log 当前错误日志
└── api.error.1.log2026-11-22T14:35:12.345Z [0] GET /api/user/profile 200 12ms
2026-11-22T14:35:12.401Z [3] TypeError: Cannot read properties of undefined
2026-11-22T14:35:13.900Z [bm2] 实例 0 pid=4021 原因=exit_code 退出码=1 重启次数=1tail -f ~/.bm2/projects/api/logs/api.log # 跟随普通日志
tail -50 ~/.bm2/projects/api/logs/api.error.log # 最近 50 行错误
grep ' 原因= ' ~/.bm2/projects/api/logs/api.error.log # 只看崩溃记录2026-11-22T12:32:20.608Z [bm2] 实例 0 pid=39335 原因=memory_limit 内存=560MB 上限=512MB 重启次数=2| 事件 | 含义 |
|---|---|
| crash | 实例崩溃,异常退出或启动失败,稍后重启 |
| errored | 自愈失败,重启次数用完,停下等人处理 |
| memory_limit | 超过 max_memory_mb,按异常重启 |
| oom_kill | 内存逼近上限时被系统杀掉 |
| reuseport_missing | 多实例没开 reusePort,项目被停 |
| lifecycle | 项目启停:启动、停止、退役、干净退出后重启 |
| daemon | 看管进程启动或换班接管项目 |
| threshold | 阈值告警:内存逼近、离线过久、重启过频 |
| disk_pressure | 状态盘剩余空间不足 |
| start_timeout | 拉起后端口一直没监听 |
| daily_stats | 每日统计 |
~/.bm2/bm2d.events.log 看管进程与监督
~/.bm2/bm2.events.log 命令侧连接与重试2026-11-22T12:32:19.379Z instance_exited app=worker instance=0 pid=39335 detail="exit_code=1"
2026-11-22T12:32:19.380Z instance_restart_scheduled app=worker instance=0 detail=exit_code| 写法 | 效果 |
|---|---|
| 不写 | 收全部事件 |
| events = ["crash", "oom_kill"] | 只收列出的 |
| events = ["!lifecycle"] | 除了列出的都收 |
| 配置键 | 含义 | 默认 |
|---|---|---|
| threshold_rss_percent | 内存占 max_memory_mb 的百分比 | 80 |
| threshold_offline_min | 实例离线多久算异常 | 5 分钟 |
| threshold_restarts | 连续重启几次算异常 | 5 |
| start_timeout_ms | 拉起后多久未监听算超时 | 30000 |
| threshold_disk_mb | 状态盘剩余空间的下限 | 1024 |
| threshold_storm | 十分钟内重启次数的上限 | 20 |
[notify]
threshold_rss_percent = 85 # 内存占上限 85% 时提醒
threshold_offline_min = 3 # 离线 3 分钟即报
threshold_storm = 0 # 关掉风暴告警
events = ["!daily_stats"]| 平台 | platform | 需要的字段 |
|---|---|---|
| 飞书 | feishu | url,可选 secret 加签 |
| 钉钉 | dingtalk | url,可选 secret 加签 |
| 企业微信 | wecom | url |
| Slack | slack | url |
| Discord | discord | url |
| 自定义接口 | webhook | url,可选 secret 作令牌 |
| 邮箱 | to,通道写在 [notify] |
[[notify.target]]
platform = "slack"
url = "__SLACK_WEBHOOK__"
format = "markdown"
events = ["crash", "errored", "memory_limit"][[notify.target]]
platform = "email"
to = ["ops@example.com"]
events = ["!lifecycle"]url = "__FEISHU_WEBHOOK__" # 真值在 .env 的 FEISHU_WEBHOOKbm2 notify~/.bm2/
├── daemon.endpoint 控制通道
├── bm2d.pid 看管进程号
├── bm2d.log 看管进程自己的输出
├── bm2d.events.log 看管事件
├── bm2.events.log 命令侧事件
└── projects/
└── api/
├── project.json 登记信息
├── state/ 每个实例的运行状态
├── logs/ 日志
└── .tempLogs/ 应用输出的暂存tail -5 ~/.bm2/projects/api/logs/api.error.log2026-11-22T12:32:20.608Z [bm2] 实例 0 pid=39335 原因=exit_code 退出码=1 重启次数=10api 正在删除中,稍等再启动Bun.serve({ port, reusePort: true, fetch: (req) => new Response("ok") })cwd = "/wwwroot/api"
script = "dist/server.js"port conflict: worker port range 4401~4401 overlaps api 4400~4401bm2 notifylog_max_size_mb = 5
log_keep_files = 3| pm2 | bm2 |
|---|---|
| pm2 start ecosystem.config.js | 项目目录里 bm2 start |
| pm2 list | bm2 list |
| pm2 logs api | tail -f ~/.bm2/projects/api/logs/api.log |
| pm2 stop api | bm2 stop api |
| pm2 restart api | bm2 restart api |
| pm2 delete api | bm2 delete api |
| pm2 flush | 由轮转自动处理 |
| pm2 startup + pm2 save | 交给 systemd |
| 指标 | pm2 7.0.4 | bm2 |
|---|---|---|
| 看管进程内存(管一个项目) | 71 MB | 5.2 MB |
| 单实例总占用(看管 + 应用) | 118 MB | 31 MB |
| 安装体积 | 25 MB | 5.1 MB |
| 运行时依赖 | Node.js 24 | 无 |
| 安装带来的包 | 70 个 npm 包 | 2 个可执行文件 |
| 重启到可用 | 229 ~ 272 ms | 113 ~ 131 ms |
| 日志轮转 | 需装 pm2-logrotate | 内置 |
| 多实例分发 | cluster master 进程 | 内核 SO_REUSEPORT |
| 配置 | ecosystem.config.js | bm2.toml,字段写错直接报 |
Install
Download zipA lightweight Linux process manager for Bun and Node.js applications, written in MoonBit