我一直试图通过编程控制多个反攻击:全球攻击专用服务器。一切正常,但我很难把它完全关掉。当您打开服务器时,它会创建两个进程:srcds_run和srcds_linux。我可以轻松地完成process.kill(),它将关闭srcds_run进程,但是即使服务器关闭,srcds_linux进程也会继续运行。如果我试图杀死所有的 srcds_linux进程,那么它将杀死CSGO服务器的all,即使我只试图关闭一个。是否有任何方法选择相应的srcds_run和srcds_linux进程?
到目前为止,这是我的代码:
// Turns on the server if not already on
Server.prototype.start = function(callback) {
if(!this.online) {
// Turn server on
console.log('Starting server');
this.process = spawn(this.directory + '/srcds_run', ['-game csgo', '-console', '-usercon', '+game_type 0', '+game_mode 0', '+mapgroup mg_active', '+map de_dust2', '+sv_setsteamaccount ' + this.token], { cwd: this.directory});
this.process.stderr.on('data', function(err) {
console.log('Error: ' + err);
});
this.process.stdin.on('data', function(chunk) {
console.log('stdin: ' + chunk);
});
this.process.stdout.on('data', function(chunk) {
console.log('stdout: ' + chunk);
});
}
this.online = true;
callback();
}
// Turns off the server if not already off
Server.prototype.stop = function(callback) {
if(this.online) {
// Turn server off
console.log('Stopping server');
this.process.kill();
}
this.online = false;
callback();
}我使用的是Ubuntu,我正在使用process.spawn模块 on node.js
(我感谢你的帮助:)
发布于 2015-12-27 05:26:17
因此,感谢@dvlsg,我使用pgrep关闭了相应的srcds_linux进程。这是我到目前为止掌握的密码。
var child_process = require('child_process');
var exec = child_process.exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr) {
callback(stdout, error, stderr);
});
};
// Turns off the server if not already off
Server.prototype.stop = function(callback) {
if(this.online) {
// Turn server off
console.log('Stopping server');
var processId = this.process.pid;
execute('pgrep -P ' + processId, function(childId, error, stderror) {
console.log('Parent ID: ' + processId);
console.log('Child ID: ' + childId);
this.process.on('exit', function(code, signal) {
console.log('Recieved event exit');
execute('kill ' + childId);
this.online = false;
callback();
});
this.process.kill();
});
} else {
this.online = false;
callback();
}
}如果我改进了代码,我将更新它,但这是我到目前为止所拥有的。
https://stackoverflow.com/questions/34445294
复制相似问题