Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs-来自连续execSync函数的意外behevior_Node.js_Docker - Fatal编程技术网

Node.js nodejs-来自连续execSync函数的意外behevior

Node.js nodejs-来自连续execSync函数的意外behevior,node.js,docker,Node.js,Docker,我在Ubuntu 16上的nodejs中运行了以下内容。要处理nodejs程序的终止: process.on('SIGINT', function () { process.exit(); // On ctrl + C, exit this process. }); // catches the exit(). On exit, execute our handler function. process.on('exit', exitHandler.bind(null, { exit:

我在Ubuntu 16上的nodejs中运行了以下内容。要处理nodejs程序的终止:

process.on('SIGINT', function () {
    process.exit(); // On ctrl + C, exit this process.
});

// catches the exit(). On exit, execute our handler function.
process.on('exit', exitHandler.bind(null, { exit: true }));

async function exitHandler(options, exitCode) {

    try {
        await execSync("docker kill $(docker ps -q -a)");
    } catch (err) {
        errOut(err);
    }
    try {
        await execSync("docker rm $(docker ps -q -a)");
    } catch (err) {
        errOut(err);
    }
    stdOut("\nExiting the process..");
}

问题是如果第一个
await execSync
正常执行,下一个
await execSync
将不会执行。但奇怪的是,如果第一个
execSync
崩溃并最终出错,第二个就会执行。我希望他们在任何情况下都能一个接一个地执行。我不知道如何解决这个问题。

命令
docker ps-q-a
返回所有容器的id,因此如果有一些容器未运行,命令
docker kill
将返回错误


要解决此问题,请将第一个execSync更改为
docker kill$(docker ps-q)

是否可以共享错误跟踪?这是一个受控错误,表示容器尚未运行,因此无法终止<代码>来自守护程序的错误响应:无法终止容器:8f5d884d9883:容器8f5d884d9883。。。未运行无法杀死未运行的容器,请将第一个
execSync
更改为
docker kill$(docker ps-q)
这是一个好建议。但是我可能已经把这个问题解释得很糟糕了,这里真正的问题是nodejs的行为与我认为的try{
await execSync
}不同。。。你应该表现好。有些进程可能还活着,我想先杀死它们,正如你提到的,没有
-a
。但是如果没有错误,我的下一条指令将不会执行。可能是您有几个容器,并且该命令需要一些时间才能完全执行