PM2 runs under its own systemd unit, never as a child of the agent

Found in the VM test: a site's PM2 daemon started from the agent's install
job belonged to the agent's service, so stopping or updating the agent took
down every site it had installed. Before any PM2 command as a site user,
the scripts now make sure that user's daemon runs under pm2-<user> (moving
one that runs elsewhere, keeping its process list). The agent's stats no
longer start an empty daemon by asking one that isn't running. Uninstall
stops the unit before removing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 16:40:12 +02:00
co-authored by Claude Opus 5.5
parent 19709c2c75
commit 570f2dda5a
4 changed files with 44 additions and 2 deletions
+16 -1
View File
@@ -1,4 +1,4 @@
import { execFile } from "node:child_process";
import { execFile, execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -87,9 +87,24 @@ export function churchProcesses(sitesDir = paths.sites): Map<string, { church: s
interface Pm2Proc { name?: string; monit?: { memory?: number; cpu?: number }; pm2_env?: { status?: string; restart_time?: number; pm_uptime?: number } }
function pm2Running(user: string): boolean {
try {
const home = execFileSync("getent", ["passwd", user], { encoding: "utf8" }).split(":")[5];
const pid = Number(fs.readFileSync(`${home}/.pm2/pm2.pid`, "utf8").trim());
if (!pid) return false;
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
async function pm2Processes(): Promise<agent.AgentStats["processes"]> {
const out: agent.AgentStats["processes"] = [];
for (const [user, installs] of churchProcesses()) {
// Only ask a daemon that's running: `pm2 jlist` would otherwise start an empty one inside this
// agent's service, which dies with the agent (found in the VM test).
if (!pm2Running(user)) continue;
const json = await run("sudo", ["-n", "-u", user, "-H", "--", "pm2", "jlist"]);
let list: Pm2Proc[];
try { list = JSON.parse(json ?? "[]") as Pm2Proc[]; } catch { list = []; }