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
+27
View File
@@ -66,8 +66,35 @@ upper() { printf '%s' "$1" | tr '[:lower:]-' '[:upper:]_'; }
# ── Running things as the app user ───────────────────────────────────────────────────────────
# run_as <user> <dir> <command…>: runs the command in <dir> as <user> (directly when we already
# are that user), with that user's HOME so PM2 and npm use their own folders.
# pm2_service: makes sure the pm2 daemon of user $1 runs under its own systemd unit (pm2-<user>),
# never as a child of whatever ran this script. A daemon started from a command (the agent's job,
# an SSH session) belongs to that command's service and dies with it: stopping or updating the
# agent took down every site it had installed (found in the VM test). Done once per user per run.
PM2_SERVICE_DONE=" "
pm2_service() {
local user="$1" home pid n
case "$PM2_SERVICE_DONE" in *" $user "*) return 0 ;; esac
PM2_SERVICE_DONE="$PM2_SERVICE_DONE$user "
{ command -v systemctl >/dev/null && [ -d /run/systemd/system ] && [ "$(id -u)" = 0 ]; } || return 0
home="$(getent passwd "$user" | cut -d: -f6)"
systemctl cat "pm2-$user" >/dev/null 2>&1 || env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$home" >/dev/null
systemctl is-active -q "pm2-$user" && return 0
pid="$(cat "$home/.pm2/pm2.pid" 2>/dev/null || true)"
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
# A daemon outside its unit: keep its list (an empty one doesn't replace the saved list),
# stop it, and let the unit bring the list back. Its sites are down for a few seconds.
n="$(sudo -u "$user" -H -- pm2 jlist 2>/dev/null | node -e 'let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => { try { console.log(JSON.parse(s).length); } catch { console.log(0); } })')"
if [ "${n:-0}" -gt 0 ]; then sudo -u "$user" -H -- pm2 save --force >/dev/null 2>&1 || true; fi
sudo -u "$user" -H -- pm2 kill >/dev/null 2>&1 || true
info "PM2 for $user now runs under systemd (pm2-$user)."
fi
systemctl start "pm2-$user" || warn "pm2-$user didn't start: check systemctl status pm2-$user."
}
run_as() {
local user="$1" dir="$2"
# Any PM2 command runs against the daemon in its own unit (see pm2_service).
if [ "$3" = pm2 ]; then pm2_service "$user"; fi
shift 2
if [ "$(id -un)" = "$user" ]; then
(cd -- "$dir" && "$@")