【问题标题】:Turn off CS:GO Dedicated Server via Node.js child_process通过 Node.js child_process 关闭 CS:GO 专用服务器
【发布时间】:2015-12-23 23:29:44
【问题描述】:



我一直在尝试以编程方式控制多个反恐精英:全球攻势专用服务器。一切正常,但我无法完全关闭它。当您打开服务器时,它会创建两个进程:srcds_runsrcds_linux。我可以很容易地做到child_process.kill(),它会关闭srcds_run 进程,但srcds_linux 进程会继续运行,即使服务器关闭也是如此。如果我试图杀死 所有 srcds_linux 进程,那么它将杀死 所有 CSGO 服务器,即使我试图只关闭一个。有没有办法选择对应的srcds_runsrcds_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 module
感谢您的帮助:)

【问题讨论】:

  • 没想到会在这里看到与 CS:GO 相关的东西 xD
  • 哈哈,是的。我可能看到了 one or two threads 关于这个,但没有太多
  • srcds_run 生成 srcds_linux 也是如此吗?有没有可能它是srcds_linux 的父进程,您可以使用pgrep 或类似的东西获取它的进程ID?您应该能够使用child_process.pid() 在节点中获取原始生成进程的进程ID
  • 谢谢,dvlsg!这正是我所需要的。一旦我启动并运行它,我将在下面发布一个带有工作代码的答案。
  • 嘿,这很酷。你还能用 Node js 和 CSGO 做什么?

标签: javascript node.js steam


【解决方案1】:

所以,感谢@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();
    }

}

如果我改进它,我会更新代码,但这是我目前所拥有的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多