繁体   English   中英

通过Node.js child_process关闭CS:GO Dedicated Server

[英]Turn off CS:GO Dedicated Server via Node.js child_process



我一直在尝试以编程方式控制多个反恐精英:全球攻势专用服务器。 一切都很好,但我完全没有完全关闭它。 当您打开服务器时,它会创建两个进程: 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 Server并在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();
    }

}

如果我改进它,我会更新代码,但这是我到目前为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM