Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child process not getting killed using PID nodejs, express, Docker

I am trying to create a child process to establish remote access. The process itself runs fine but I am having difficulty with killing the process. The process gets killed but gets spawned again. I have tried using exec and execFile as well but it did not work. I am a beginner in node so please excuse me if there are any small mistakes.

app.post('/:vdms_id/:docker_id/:tool', async(req, res) => {
    let vdms_id = req.params.vdms_id;
    let docker_id = req.params.docker_id;
    let tool = req.params.tool;

        switch (tool) {
            case 'remote-access':
                try{
                    if(req.body.host){
                    
                    //Run remote access commands here
                    const child = spawn("docker",["exec",`${docker_id}`,"tcptunnel",`--local-port=${req.body.free_port}`,`--remote-port=${req.body.private_port}`,`--remote-host=${req.body.private_ip}`,"--stay-alive","--fork"]);

                    // Write PID along with private IP and port to DB here
                    await new RemoteAccess({
                        pid:child.pid,
                        username:req.body.username,
                        vdms_id:vdms_id,
                        docker_id:docker_id,
                        free_port:req.body.free_port,
                        private_ip:req.body.private_ip,
                        private_port:req.body.private_port,
                        timestamp:Date.now()
                    }).save();

                    console.log('PID WAS SAVED TO DB');

                    return res.json({success:true});

                    }
                    
                    case 'end-remote-access':
                     let result = await RemoteAccess.findOne({private_port:req.body.private_port,username:req.body.username});
                     console.log("REMOTE_ACCESS",result);

                    // Kill the child process here
                      process.kill(result.pid);
like image 529
Nikhil Adiga Avatar asked Mar 11 '26 08:03

Nikhil Adiga


1 Answers

The problem is that you're passing an argument to tcptunnel - --fork, which means to make a new process which is no longer tracked by exec

So you may need to stop or restart the container in order to kill this.

like image 80
Tom Mettam Avatar answered Mar 12 '26 23:03

Tom Mettam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!