首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Node.js child_process关闭CS:GO专用服务器

通过Node.js child_process关闭CS:GO专用服务器
EN

Stack Overflow用户
提问于 2015-12-23 23:29:44
回答 1查看 1K关注 0票数 3

我一直试图通过编程控制多个反攻击:全球攻击专用服务器。一切正常,但我很难把它完全关掉。当您打开服务器时,它会创建两个进程:srcds_runsrcds_linux。我可以轻松地完成process.kill(),它将关闭srcds_run进程,但是即使服务器关闭,srcds_linux进程也会继续运行。如果我试图杀死所有的 srcds_linux进程,那么它将杀死CSGO服务器的all,即使我只试图关闭一个。是否有任何方法选择相应的srcds_runsrcds_linux进程?

到目前为止,这是我的代码:

代码语言:javascript
复制
// 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

(我感谢你的帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-27 05:26:17

因此,感谢@dvlsg,我使用pgrep关闭了相应的srcds_linux进程。这是我到目前为止掌握的密码。

代码语言:javascript
复制
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();
    }

}

如果我改进了代码,我将更新它,但这是我到目前为止所拥有的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34445294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档