fix restart error

This commit is contained in:
musistudio
2026-01-06 14:23:46 +08:00
parent b1e057f9f2
commit 4afa571934
2 changed files with 3 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
import { run } from "./utils"; import { run, restartService } from "./utils";
import { showStatus } from "./utils/status"; import { showStatus } from "./utils/status";
import { executeCodeCommand, PresetConfig } from "./utils/codeCommand"; import { executeCodeCommand, PresetConfig } from "./utils/codeCommand";
import { import {
@@ -430,39 +430,7 @@ async function main() {
console.log(`claude-code-router version: ${version}`); console.log(`claude-code-router version: ${version}`);
break; break;
case "restart": case "restart":
// Stop the service if it's running await restartService();
try {
const pid = parseInt(readFileSync(PID_FILE, "utf-8"));
process.kill(pid);
cleanupPidFile();
if (existsSync(REFERENCE_COUNT_FILE)) {
try {
fs.unlinkSync(REFERENCE_COUNT_FILE);
} catch (e) {
// Ignore cleanup errors
}
}
console.log("claude code router service has been stopped.");
} catch (e) {
console.log("Service was not running or failed to stop.");
cleanupPidFile();
}
// Start the service again in the background
console.log("Starting claude code router service...");
const cliPath = join(__dirname, "cli.js");
const startProcess = spawn("node", [cliPath, "start"], {
detached: true,
stdio: "ignore",
});
startProcess.on("error", (error) => {
console.error("Failed to start service:", error);
process.exit(1);
});
startProcess.unref();
console.log("✅ Service started successfully in the background.");
break; break;
case "-h": case "-h":
case "help": case "help":

View File

@@ -238,7 +238,7 @@ export const restartService = async () => {
// Start the service again in the background // Start the service again in the background
console.log("Starting claude code router service..."); console.log("Starting claude code router service...");
const cliPath = path.join(__dirname, "../cli.js"); const cliPath = path.join(__dirname, "cli.js");
const startProcess = spawn("node", [cliPath, "start"], { const startProcess = spawn("node", [cliPath, "start"], {
detached: true, detached: true,
stdio: "ignore", stdio: "ignore",