1

I'm using

router.post('/create', function(req, res) {
    pool.query("SELECT MAX(server_port) FROM servers", function(err, rows) {
        if (err) return console.log(err);
        var serverPort = parseInt(rows[0]['MAX(server_port)']) + 1;
        var decoder = new StringDecoder('utf8')
        var child = spawn('/home/steam/steamcmd/csgo/srcds_run',
        ['-game csgo' ,'-console' ,'+game_type 0' ,'+game_mode ' + req.body.serverType, '+map ' + req.body.serverMap,
        '-tickrate ' + req.body.tickRate, '+maxplayers ' + req.body.maxPlayers,
        '+hostname ' + req.body.serverName, '+port ' + serverPort, '+password '],
        {
            detached: true
        });
        child.stderr.on('data', function(err) {
            console.log(err);
        });
        child.stdout.on('data', function(chunk) {
        });

        console.log(child.pid);
        var inserts = {
            server_name: req.body.serverName || 'CSGO Quick Server',
            server_players: req.body.maxPlayers,
            server_owned_by: req.session.username,
            serverPID: child.pid,
            server_port: serverPort,
            server_tickrate: req.body.tickRate,
            server_map: req.body.serverMap,
            server_type: req.body.serverType
        };
        pool.query("INSERT INTO `servers` SET ?", inserts, function(err, insert) {
           if (err) return console.log(err);
           res.json({'success': true});
        });
    });
});

to create a spawn for CS:GO (video game) to run a server from the front-end, and that is working great, the server goes up and runs. But I have to run the program ./srcds_run which sets some variables and then runs ./srcds_linux so I keep track of the PID of ./srcds_run but when I go to kill it, the ./srcds_linux still runs and I'm not sure how to get the PID of it.

Trying to killall won't work because there might be another ./srcds_linux running that I do not want to kill.

Basically what I am asking is, how do I kill the process that was created by ./srcds_run.

This is how I'm destroying it:

router.get('/destroy', function(req, res) {
    if (req.session.hasOwnProperty('username')) {
        pool.query("SELECT * FROM servers WHERE server_owned_by = ?", [req.session.username], function(err, rows) {
           if (err) return res.redirect('/');
           if (rows.length >= 1) {
               console.log('here');
                exec('kill ' + rows[0].serverPID, function(err, stdout, stderr) {
                   if (err) return console.log(err);
                   if (stderr) return console.log(stderr);
                   console.log(stdout);
                   pool.query("DELETE FROM servers WHERE serverPID = ?", [rows[0].serverPID], function(err) {
                      if (err) console.log(err);
                      res.redirect('/servers');
                   });
                });
           }
        });
    } else {
        res.redirect('/');
    }
});

And this is what I end up with

root@PanicWoW:~# ps aux | grep csgo
root     16995  0.0  0.0   4440   704 ?        Ss   06:45   0:00 /bin/sh /home/steam/steamcmd/csgo/srcds_run -game csgo -console +game_type 0 +game_mode 0 +map de_dust2 -tickrate 64 +maxplayers 10 +hostname  +port 27016 +password 
root     17022  6.3  6.4 262072 132960 ?       Sl   06:45   0:05 ./srcds_linux -game csgo -console +game_type 0 +game_mode 0 +map de_dust2 -tickrate 64 +maxplayers 10 +hostname +port 27016 +password

So when I kill 16995, the other process continues to run instead of dying with it, even though 16995 created it.

Sorry if this is confusing I'm not sure how to word this as it is fairly new area for me (not the JS, but dealing with child_processes and linux) thanks a lot. If I can clarify anything please ask.

Datsik
  • 14,453
  • 14
  • 80
  • 121
  • I think this is a much more linux aligned question instead of being related to node.js, perhaps taking a look at the following SO question might she some light to answer your question: http://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes – Ma'moon Al-Akash May 06 '15 at 12:07

1 Answers1

0

So it turns out that the Process group ID is the same as the first process, so by using KILL -TERM -#### I can kill the entire tree.

Datsik
  • 14,453
  • 14
  • 80
  • 121