fix: soften windows daemon install
This commit is contained in:
@@ -26,6 +26,15 @@ function quoteCmdArg(value: string): string {
|
||||
return `"${value.replace(/"/g, '\\"')}"`;
|
||||
}
|
||||
|
||||
function resolveTaskUser(env: Record<string, string | undefined>): string | null {
|
||||
const username = env.USERNAME || env.USER || env.LOGNAME;
|
||||
if (!username) return null;
|
||||
if (username.includes("\\")) return username;
|
||||
const domain = env.USERDOMAIN;
|
||||
if (domain) return `${domain}\\${username}`;
|
||||
return username;
|
||||
}
|
||||
|
||||
function parseCommandLine(value: string): string[] {
|
||||
const args: string[] = [];
|
||||
let current = "";
|
||||
@@ -216,7 +225,7 @@ export async function installScheduledTask({
|
||||
|
||||
const taskName = resolveGatewayWindowsTaskName(env.CLAWDBOT_PROFILE);
|
||||
const quotedScript = quoteCmdArg(scriptPath);
|
||||
const create = await execSchtasks([
|
||||
const baseArgs = [
|
||||
"/Create",
|
||||
"/F",
|
||||
"/SC",
|
||||
@@ -227,9 +236,20 @@ export async function installScheduledTask({
|
||||
taskName,
|
||||
"/TR",
|
||||
quotedScript,
|
||||
]);
|
||||
];
|
||||
const taskUser = resolveTaskUser(env);
|
||||
let create = await execSchtasks(
|
||||
taskUser ? [...baseArgs, "/RU", taskUser, "/NP", "/IT"] : baseArgs,
|
||||
);
|
||||
if (create.code !== 0 && taskUser) {
|
||||
create = await execSchtasks(baseArgs);
|
||||
}
|
||||
if (create.code !== 0) {
|
||||
throw new Error(`schtasks create failed: ${create.stderr || create.stdout}`.trim());
|
||||
const detail = create.stderr || create.stdout;
|
||||
const hint = /access is denied/i.test(detail)
|
||||
? " Run PowerShell as Administrator or rerun without installing the daemon."
|
||||
: "";
|
||||
throw new Error(`schtasks create failed: ${detail}${hint}`.trim());
|
||||
}
|
||||
|
||||
await execSchtasks(["/Run", "/TN", taskName]);
|
||||
|
||||
Reference in New Issue
Block a user