I've been trying to programmatically control multiple Counter Strike: Global Offensive dedicated servers. Everything works fine, but I'm having trouble turning it completely off. When you turn on the server, it creates two processes: srcds_run
and srcds_linux
. I can easily do child_process.kill(), and it will turn off the srcds_run
process, but the srcds_linux
process keeps on running, even when the server is off. If I try to kill all srcds_linux
processes, then it will kill all of the CSGO servers, even if I'm trying to turn off only one. Is there any way to select the corresponding srcds_run
and srcds_linux
processes?
This is my code so far:
// 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();
}
I'm using an Ubuntu Server and am using the child_process.spawn module on node.js
I appreciate the help :)
So, thanks to @dvlsg, I'm using pgrep
to shut down the corresponding srcds_linux
process. This is the code I have so far.
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();
}
}
I will update the code if I improve it, but this is what I have so far.