gpt4 book ai didi

What happens to child process once the shell command is completed?(一旦外壳命令完成,子进程会发生什么情况?)

转载 作者:bug小助手 更新时间:2023-10-24 23:24:38 25 4
gpt4 key购买 nike




So i am using exec to open a shell and run a shell command for me which is:

因此,我使用EXEC打开一个外壳,并为我运行一个外壳命令:


exec('del filename & exit')

Using exit to close the Shell

使用退出关闭外壳


I have to use this exec multiple times in a single run, i have not been able to find what happens to the child process once it goes to exit and closes the CMD, does the child process close itself as soon as the command is completed or do i have to explicitly close the child process

我必须在一次运行中多次使用此EXEC,我还无法查找子进程退出并关闭CMD后会发生什么,子进程是在命令完成后立即自行关闭还是必须显式关闭子进程


更多回答

del filename & exit looks like a Windows Command Processor command line. There is executed in this case %ComSpec% /c del filename & exit which expands to C:\WINDOWS\system32\cmd.exe /c del filename & exit. Open a command prompt window, run cmd /? and read the output usage help explaining how the arguments after option /C are interpreted by cmd depending on various criteria. The option /c instructs cmd to execute the command line and then close itself. & exit is completely unnecessary for that reason. The file in the current directory is deleted if possible by cmd.

DEL文件名和退出看起来像是Windows命令处理器命令行。在本例中,将执行%ComSpec%/c del FileName&Exit,它将展开到C:\WINDOWS\SYSTEM32\cmd.exe/c del Filename&Exit。打开命令提示符窗口,运行cmd/?并阅读输出用法帮助,解释cmd如何根据各种条件解释选项/C之后的参数。选项/c指示cmd执行命令行,然后自行关闭。因此,完全不需要退出(&EXIT)。如果可能,cmd会删除当前目录中的文件。

If the Node.js script itself does not determine which directory is the current directory, then the process starting node.exe for interpreting the Node.js script defines the current working directory on calling the Windows kernel library function CreateProcess for execution of node.exe with the Node.js script file name as argument. Any directory can be therefore the current directory on node.exe calling CreateProcess to run cmd.exe with the arguments.

如果Node.js脚本本身不确定哪个目录是当前目录,则启动node.exe以解释Node.js脚本的进程将在调用Windows内核库函数CreateProcess以执行node.exe(以Node.js脚本文件名为参数)时定义当前工作目录。因此,任何目录都可以是node.exe上的当前目录,调用CreateProcess以使用参数运行cmd.exe。

优秀答案推荐

UNIX exec (POSIX system call)

Unix EXEC(POSIX系统调用)


There is no child process involved in the picture.

图片中没有涉及任何子进程。


Per the linux man pages,

根据Linux手册页,



One sometimes sees execve() (and the related functions described
in exec(3)) described as "executing a new process" (or similar).
This is a highly misleading description: there is no new process;
many attributes of the calling process remain unchanged (in
particular, its PID). All that execve() does is arrange for an
existing process (the calling process) to execute a new program.



Instead, the exec() family of functions replaces the current process image with a new process image. see: man pages

相反,exec()系列函数用新的进程映像替换当前的进程映像。请参阅:手册页






Node.js exec (Child process module)

Node.js exec(子进程模块)


Unlike the POSIX exec, this function DOES spawn a new process.

与POSIX EXEC不同,该函数确实会产生一个新进程。


Per the docs,

根据医生的说法,



Unlike the exec(3) POSIX system call, child_process.exec() does not replace the existing process and uses a shell to execute the command.



To answer your question,

为了回答你的问题,



does the child process close itself as soon as the command is completed or do i have to explicitly close the child process



The child process is expected to be terminated automatically by the ChildProcess instance, but there is no guarantee that the process will terminate at all costs.

该子进程预计将由ChildProcess实例自动终止,但不能保证该进程将不惜一切代价终止。


Per docs, several events are emitted upon resolution of a child process,

对于每个文档,在解析子进程时发出几个事件,



  1. Event 'exit':


When the 'exit' event is triggered, child process stdio streams might still be open.

当‘Exit’事件被触发时,子进程Stdio Streams可能仍处于打开状态。


child.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
});

Note that, the 'exit' event may or may not fire after an error has occurred. When listening to both the 'exit' and 'error' events, guard against accidentally invoking handler functions multiple times.

请注意,在发生错误后,‘Exit’事件可能会触发,也可能不会触发。同时侦听‘Exit’和‘Error’事件时,要防止多次意外调用处理程序函数。





  1. Event 'error':


The 'error' event is emitted whenever:

每当出现以下情况时,都会发出‘Error’事件:



  • The process could not be spawned.

  • The process could not be killed.

  • Sending a message to the child process failed.

  • The child process was aborted via the signal option.


child.on('error', (err) => {
// This will be called with err being an AbortError if the controller aborts
});




  1. Event 'close':


The 'close' event is emitted after a process has ended and the stdio streams of a child process have been closed. This is distinct from the 'exit' event, since multiple processes might share the same stdio streams. The 'close' event will always emit after 'exit' was already emitted, or 'error' if the child failed to spawn.

‘Close’事件在进程结束并且子进程的Stdio流已关闭后发出。这与‘Exit’事件不同,因为多个进程可能共享相同的Stdio流。“Close”事件将始终在“Exit”已发出后发出,如果子级未能派生,则将发出“Error”。




Additionally, a timeout option and a killSignal can be specified to a process for it to be killed by the Node.js runtime using the specified signal if it does not exit in the specified timeout period.

此外,如果进程没有在指定的超时周期内退出,则可以为进程指定一个超时选项和一个KillSignal,以便Node.js运行时使用指定的信号终止该进程。


If timeout is greater than 0, the parent will send the signal identified by the killSignal property (the default is 'SIGTERM') if the child runs longer than timeout milliseconds.

如果超时大于0,并且子进程运行的时间超过超时毫秒,则父进程将发送KILL Signal属性标识的信号(默认为‘SIGTERM’)。


You can pass these options as the 2nd parameter to the exec method as follows:

您可以将这些选项作为第二个参数传递给exec方法,如下所示:


child_process.exec('del filename', {
timeout: 1000, //milliseconds
killSignal: "SIGINT" //POSIX signal name
});

Final note on these handlers:

关于这些处理程序的最后注意事项:



Node.js establishes signal handlers for SIGINT and SIGTERM and Node.js processes will not terminate immediately due to receipt of those signals. Rather, Node.js will perform a sequence of cleanup actions and then will re-raise the handled signal.



更多回答

While the man page is exec(3) there is no Unix/POSIX call named exactly exec (as shown at your second link) and moreover no Unix/POSIX call handles the argument shown in the Q. And the Q is tagged nodejs -- so it is clearly about the exec function in the child_process module of nodejs which DOES create a child process -- like fork PLUS execX or posix_spawnX on Unix/POSIX, but different on Windows which I'm pretty sure is the Q.

虽然手册页是exec(3),但没有完全命名为exec的Unix/POSIX调用(如您的第二个链接所示),而且也没有Unix/POSIX调用处理Q中显示的参数。Q标记为NodeJS--因此它显然是关于NodeJS的Child_process模块中的exec函数,它确实创建了一个子进程--如Unix/POSIX上的fork plus execX或POSIX_spawnX,但在Windows上不同,我非常确定它就是Q。

You are right. I jumped right into the question and clearly have omitted the tags! Funny that neither the question nor the title doesn't mention anything about node.js environment, which clearly what the "Q" is about. In that case, I would be happy if you can update the answer or create a new one, which in that case I will remove mine. --Thanks

你是正确的。我直接跳到问题中,显然省略了标签!有趣的是,问题和标题都没有提到有关node.js环境的任何内容,这清楚地说明了“q”是关于什么。在这种情况下,如果你能更新答案或创建一个新的答案,我会很高兴,在这种情况下,我会删除我的答案。--谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com