fix(daemon): handle systemctl is-enabled exit code 4 (not-found) on Ubuntu (#33633)

This commit is contained in:
yuandiaodiaodiao
2026-03-04 07:50:23 +08:00
committed by Shakker
parent 3fa43ec221
commit d1ad9e2753
2 changed files with 19 additions and 1 deletions

View File

@@ -90,6 +90,21 @@ describe("isSystemdServiceEnabled", () => {
"systemctl is-enabled unavailable: Failed to connect to bus",
);
});
it("returns false when systemctl is-enabled exits with code 4 (not-found)", async () => {
const { isSystemdServiceEnabled } = await import("./systemd.js");
execFileMock.mockImplementationOnce((_cmd, _args, _opts, cb) => {
// On Ubuntu 24.04, `systemctl --user is-enabled <unit>` exits with
// code 4 and prints "not-found" to stdout when the unit doesn't exist.
const err = new Error(
"Command failed: systemctl --user is-enabled openclaw-gateway.service",
) as Error & { code?: number };
err.code = 4;
cb(err, "not-found\n", "");
});
const result = await isSystemdServiceEnabled({ env: {} });
expect(result).toBe(false);
});
});
describe("systemd runtime parsing", () => {

View File

@@ -143,7 +143,10 @@ async function execSystemctl(
}
function readSystemctlDetail(result: { stdout: string; stderr: string }): string {
return (result.stderr || result.stdout || "").trim();
// Concatenate both streams so pattern matchers (isSystemdUnitNotEnabled,
// isSystemctlMissing) can see the unit status from stdout even when
// execFileUtf8 populates stderr with the Node error message fallback.
return `${result.stderr} ${result.stdout}`.trim();
}
function isSystemctlMissing(detail: string): boolean {