cc_site_maker / kill-server.js
postApocalebtic
Local Server Fix
1f0573c
Raw History Blame
1.61 kB
// Script to kill any existing processes on port 7860
import { execSync } from 'child_process';
const PORT = 7860;
try {
console.log(`Checking for processes using port ${PORT}...`);
// Different commands based on OS
let command;
if (process.platform === 'win32') {
command = `netstat -ano | findstr :${PORT}`;
} else {
command = `lsof -i :${PORT} | grep LISTEN | awk '{print $2}'`;
}
const result = execSync(command, { encoding: 'utf8' });
if (result) {
console.log(`Found processes using port ${PORT}. Attempting to kill...`);
if (process.platform === 'win32') {
// Extract PID from Windows netstat output
const pidMatches = result.match(/\s+(\d+)$/m);
if (pidMatches && pidMatches[1]) {
const pid = pidMatches[1];
execSync(`taskkill /F /PID ${pid}`);
console.log(`Killed process with PID ${pid}`);
}
} else {
// On Unix systems, kill processes directly from lsof output
const pids = result.trim().split('\n');
for (const pid of pids) {
if (pid) {
execSync(`kill -9 ${pid}`);
console.log(`Killed process with PID ${pid}`);
}
}
}
console.log(`Port ${PORT} is now available`);
} else {
console.log(`No processes found using port ${PORT}`);
}
} catch (error) {
// If no processes are found, lsof/netstat might return non-zero exit code
if (error.status !== 0 && !error.stdout && !error.stderr) {
console.log(`No processes found using port ${PORT}`);
} else {
console.error(`Error checking port ${PORT}:`, error.message);
}
}