mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 01:28:27 +00:00
refactor(daemon): share systemd service action flow
This commit is contained in:
@@ -253,6 +253,22 @@ export async function uninstallSystemdService({
|
||||
}
|
||||
}
|
||||
|
||||
async function runSystemdServiceAction(params: {
|
||||
stdout: NodeJS.WritableStream;
|
||||
env?: Record<string, string | undefined>;
|
||||
action: "stop" | "restart";
|
||||
label: string;
|
||||
}) {
|
||||
await assertSystemdAvailable();
|
||||
const serviceName = resolveSystemdServiceName(params.env ?? {});
|
||||
const unitName = `${serviceName}.service`;
|
||||
const res = await execSystemctl(["--user", params.action, unitName]);
|
||||
if (res.code !== 0) {
|
||||
throw new Error(`systemctl ${params.action} failed: ${res.stderr || res.stdout}`.trim());
|
||||
}
|
||||
params.stdout.write(`${formatLine(params.label, unitName)}\n`);
|
||||
}
|
||||
|
||||
export async function stopSystemdService({
|
||||
stdout,
|
||||
env,
|
||||
@@ -260,14 +276,12 @@ export async function stopSystemdService({
|
||||
stdout: NodeJS.WritableStream;
|
||||
env?: Record<string, string | undefined>;
|
||||
}): Promise<void> {
|
||||
await assertSystemdAvailable();
|
||||
const serviceName = resolveSystemdServiceName(env ?? {});
|
||||
const unitName = `${serviceName}.service`;
|
||||
const res = await execSystemctl(["--user", "stop", unitName]);
|
||||
if (res.code !== 0) {
|
||||
throw new Error(`systemctl stop failed: ${res.stderr || res.stdout}`.trim());
|
||||
}
|
||||
stdout.write(`${formatLine("Stopped systemd service", unitName)}\n`);
|
||||
await runSystemdServiceAction({
|
||||
stdout,
|
||||
env,
|
||||
action: "stop",
|
||||
label: "Stopped systemd service",
|
||||
});
|
||||
}
|
||||
|
||||
export async function restartSystemdService({
|
||||
@@ -277,14 +291,12 @@ export async function restartSystemdService({
|
||||
stdout: NodeJS.WritableStream;
|
||||
env?: Record<string, string | undefined>;
|
||||
}): Promise<void> {
|
||||
await assertSystemdAvailable();
|
||||
const serviceName = resolveSystemdServiceName(env ?? {});
|
||||
const unitName = `${serviceName}.service`;
|
||||
const res = await execSystemctl(["--user", "restart", unitName]);
|
||||
if (res.code !== 0) {
|
||||
throw new Error(`systemctl restart failed: ${res.stderr || res.stdout}`.trim());
|
||||
}
|
||||
stdout.write(`${formatLine("Restarted systemd service", unitName)}\n`);
|
||||
await runSystemdServiceAction({
|
||||
stdout,
|
||||
env,
|
||||
action: "restart",
|
||||
label: "Restarted systemd service",
|
||||
});
|
||||
}
|
||||
|
||||
export async function isSystemdServiceEnabled(args: {
|
||||
|
||||
Reference in New Issue
Block a user