Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat: implement automated Windows service installation, uninstallation, and status management using Task Scheduler

Salman Qureshie01694d69a85
diff --git a/src/cli/daemon.ts b/src/cli/daemon.ts
index 40ef15e3..633b53bd 100644
--- a/src/cli/daemon.ts
+++ b/src/cli/daemon.ts
@@ -62,12 +62,14 @@ export function startBackground(): void {
}
const logFile = logPath();
+ const isWin = process.platform === 'win32';
const outFd = openSync(logFile, 'a');
const child = spawn(process.execPath, [process.argv[1], 'start', '--daemon'], {
detached: true,
stdio: ['ignore', outFd, outFd],
env: { ...process.env },
+ windowsHide: isWin,
});
child.unref();
@@ -99,7 +101,11 @@ export function stopDaemon(): void {
}
try {
- process.kill(status.pid, 'SIGTERM');
+ if (process.platform === 'win32') {
+ process.kill(status.pid);
+ } else {
+ process.kill(status.pid, 'SIGTERM');
+ }
console.log(chalk.green(` Mercury stopped (PID: ${status.pid})`));
} catch {
console.log(chalk.red(` Failed to stop PID ${status.pid}. You may need to kill it manually.`));
diff --git a/src/cli/service.ts b/src/cli/service.ts
index 4ac12f44..9be040ee 100644
--- a/src/cli/service.ts
+++ b/src/cli/service.ts
@@ -25,13 +25,7 @@ export function installService(): void {
} else if (platform === 'linux') {
installLinux();
} else if (platform === 'win32') {
- console.log(chalk.yellow(' Windows service install is not yet automated.'));
- console.log(chalk.dim(' Use \`mercury start -d\` for background mode, or install PM2:'));
- console.log(chalk.dim(' npm i -g pm2'));
- console.log(chalk.dim(' pm2 start mercury'));
- console.log(chalk.dim(' pm2 startup'));
- console.log('');
- return;
+ installWindows();
} else {
console.log(chalk.red(` Unsupported platform: ${platform}`));
process.exit(1);
@@ -46,8 +40,7 @@ export function uninstallService(): void {
} else if (platform === 'linux') {
uninstallLinux();
} else if (platform === 'win32') {
- console.log(chalk.yellow(' Windows service uninstall is not yet automated.'));
- console.log('');
+ uninstallWindows();
} else {
console.log(chalk.red(` Unsupported platform: ${platform}`));
process.exit(1);
@@ -62,8 +55,7 @@ export function showServiceStatus(): void {
} else if (platform === 'linux') {
showLinuxStatus();
} else if (platform === 'win32') {
- console.log(chalk.yellow(' Windows service status is not yet automated.'));
- console.log('');
+ showWindowsStatus();
}
}
@@ -294,4 +286,69 @@ function showLinuxStatus(): void {
console.log(chalk.dim(` ${err.message || err}`));
}
console.log('');
+}
+
+const WIN_TASK_NAME = 'MercuryAgent';
+
+function installWindows(): void {
+ const nodeBin = getNodeBinPath();
+ const scriptPath = getDistPath();
+ const home = getMercuryHome();
+ const logPath = join(home, 'daemon.log');
+
+ const cmd = `"${nodeBin}" "${scriptPath}" start --daemon`;
+
+ try {
+ execSync(
+ `schtasks /create /tn "${WIN_TASK_NAME}" /tr "${cmd}" /sc onlogon /rl limited /f`,
+ { stdio: 'inherit', shell: 'cmd.exe' }
+ );
+ } catch {
+ console.log(chalk.yellow(' schtasks create failed. Try running from an Administrator cmd:'));
+ console.log(chalk.dim(` schtasks /create /tn "${WIN_TASK_NAME}" /tr "${cmd}" /sc onlogon /rl limited /f`));
+ }
+
+ try {
+ execSync(`schtasks /run /tn "${WIN_TASK_NAME}"`, { stdio: 'inherit', shell: 'cmd.exe' });
+ } catch {
+ console.log(chalk.yellow(' Task created but failed to start immediately. It will start on next login.'));
+ }
+
+ console.log('');
+ console.log(chalk.green(' Mercury service installed (Windows Task Scheduler)'));
+ console.log(chalk.dim(` Task: ${WIN_TASK_NAME}`));
+ console.log(chalk.dim(` Trigger: on logon`));
+ console.log(chalk.dim(` Logs: ${logPath}`));
+ console.log(chalk.dim(' Auto-starts on login. Use --daemon flag for crash recovery.'));
+ console.log('');
+ console.log(chalk.dim(' Uninstall: mercury service uninstall'));
+ console.log('');
+}
+
+function uninstallWindows(): void {
+ try {
+ execSync(`schtasks /delete /tn "${WIN_TASK_NAME}" /f`, { stdio: 'inherit', shell: 'cmd.exe' });
+ console.log('');
+ console.log(chalk.green(' Mercury service uninstalled'));
+ console.log('');
+ } catch {
+ console.log(chalk.yellow(' Task not found or failed to delete. Remove manually:'));
+ console.log(chalk.dim(` schtasks /delete /tn "${WIN_TASK_NAME}" /f`));
+ console.log('');
+ }
+}
+
+function showWindowsStatus(): void {
+ try {
+ const output = execSync(`schtasks /query /tn "${WIN_TASK_NAME}" /fo list`, {
+ encoding: 'utf-8',
+ shell: 'cmd.exe',
+ }).trim();
+ console.log(output);
+ console.log('');
+ } catch {
+ console.log(chalk.yellow(' Mercury service is not installed.'));
+ console.log(chalk.dim(' Run `mercury service install` to set it up.'));
+ console.log('');
+ }
}
\ No newline at end of file