我正在尝试创建子进程来建立远程访问。该进程本身运行良好,但我在终止该进程时遇到了困难。该进程会被终止,但又会再次产生。我也尝试过使用exec和execFile,但都不起作用。我是node的初学者,如果有什么小错误,请原谅。
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);发布于 2020-08-11 17:21:27
问题是您向tcptunnel - --fork传递了一个参数,这意味着创建一个不再被exec跟踪的新进程
因此,您可能需要停止或重新启动容器才能终止此操作。
https://stackoverflow.com/questions/63322492
复制相似问题