通过Node.js子进程关闭CS:GO专用服务器
Turn off CS:GO Dedicated Server via Node.js child_process
我一直在尝试以编程方式控制多个Counter Strike:Global Offensive专用服务器。一切都很好,但我很难完全关闭它。当你打开服务器时,它会创建两个进程:srcds_run
和srcds_linux
。我可以很容易地执行child_process.kill(),它会关闭srcds_run
进程,但srcds_linux
进程仍在运行,即使服务器关闭。如果我试图杀死所有srcds_linux
进程,那么它会杀死的所有CSGO服务器,即使我只想关闭一个。有没有办法选择相应的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服务器,在node.js上使用child_process.spawn模块
我感谢您的帮助:)
因此,多亏了@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();
}
}
如果我改进了代码,我会更新它,但这就是我迄今为止所拥有的。
相关文章:
- 如何检测用于WebGL的专用或集成显卡
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- CSS中的游标属性似乎不适用于USB On The Go
- Twitter应用程序专用身份验证
- 关闭谷歌地图中推荐的骑行道路,但保留专用自行车道
- 在$state.go之后执行$window.location.reload(true)
- 使用Angularjs$state.go导航
- 如果没有历史记录,我如何隐藏图像或按钮onclick=;历史.go(-1);
- Meteor JS Router.go重定向到同一页面
- 评论的含义是什么;Go Horse'在编码方面?这是某种过程吗
- $state.go没有兑现承诺
- AES-CTR 在 Go 中加密,在 CryptoJS 中解密
- GO - html 文件无法加载外部 js 文件
- Array.prototype.fill() different from fill as I go
- 辅助功能键盘专用选项卡到z索引“;返回顶部”;
- 将此对象存储在自定义专用变量中
- 带有离子导航视图的Ionic中的$state.go不起作用
- AngularJS和Go POST请求方法不允许
- 如何在谷歌地图服务的Go客户端中使用方向响应
- 通过Node.js子进程关闭CS:GO专用服务器