63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
|
|
/**
|
|||
|
|
* 超简单的进程终止工具
|
|||
|
|
* 直接指定进程ID列表,避免复杂查询
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
require('dotenv').config();
|
|||
|
|
const mysql = require('mysql2/promise');
|
|||
|
|
|
|||
|
|
async function killProcessIds(ids) {
|
|||
|
|
let connection;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
connection = await mysql.createConnection({
|
|||
|
|
host: process.env.DB_HOST,
|
|||
|
|
port: process.env.DB_PORT || 3306,
|
|||
|
|
user: process.env.DB_USER,
|
|||
|
|
password: process.env.DB_PASSWORD,
|
|||
|
|
database: process.env.DB_DATABASE
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
console.log('✅ 已连接到数据库\n');
|
|||
|
|
|
|||
|
|
if (!ids || ids.length === 0) {
|
|||
|
|
console.log('⚠️ 没有提供要终止的进程ID');
|
|||
|
|
console.log('\n使用方法:');
|
|||
|
|
console.log(' node scripts/kill-by-ids.js 123 456 789');
|
|||
|
|
console.log(' 或在代码中修改 processIds 数组\n');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log(`🔪 准备终止 ${ids.length} 个进程: ${ids.join(', ')}\n`);
|
|||
|
|
|
|||
|
|
let killed = 0;
|
|||
|
|
for (const id of ids) {
|
|||
|
|
try {
|
|||
|
|
await connection.query(`KILL ${id}`);
|
|||
|
|
console.log(`✅ 已终止进程 ${id}`);
|
|||
|
|
killed++;
|
|||
|
|
} catch (err) {
|
|||
|
|
console.log(`⚠️ 进程 ${id} 终止失败: ${err.message}`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log(`\n✅ 成功终止 ${killed}/${ids.length} 个进程\n`);
|
|||
|
|
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ 操作失败:', error.message);
|
|||
|
|
process.exit(1);
|
|||
|
|
} finally {
|
|||
|
|
if (connection) {
|
|||
|
|
await connection.end();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 从命令行参数获取进程ID
|
|||
|
|
const processIds = process.argv.slice(2).map(id => parseInt(id)).filter(id => !isNaN(id));
|
|||
|
|
|
|||
|
|
// 或者直接在这里列出要终止的进程ID(如果知道的话)
|
|||
|
|
// const processIds = [123, 456, 789];
|
|||
|
|
|
|||
|
|
killProcessIds(processIds);
|