gpt4 book ai didi

linux - 通过&&运行命令时docker容器中进程的PID

转载 作者:太空宇宙 更新时间:2023-11-04 12:21:42 24 4
gpt4 key购买 nike

我写了一个 DOCKERFILE 并使用 CMD ["/bin/bash", "-c", "script1.sh && script2.sh" 启动容器。

运行一个容器后,发现PID为1的进程是一个CMD列为bash的进程,script2.sh是它的子进程。

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD            
4 S 0 1 0 0 80 0 - 4491 wait ? 00:00:00 bash
0 S 0 8 1 0 80 0 - 4494 wait ? 00:00:00 script2
0 S 0 10 8 99 80 0 - 1632452 futex_ ? 00:01:05 java
4 S 0 64 0 0 80 0 - 4545 wait ? 00:00:00 bash
0 R 0 79 64 0 80 0 - 1785 - ? 00:00:00 ps

如果我使用 CMD [ "/bin/bash", "-c", "script2.sh"],PID 为 1 的进程是 script2.sh。

谁能帮忙解释一下第一种情况会发生什么?

最佳答案

在这两种情况下,bash 都以 PID 1 开始。

对于第一个示例,bash 将运行命令 "script1.sh && script2.sh",首先调用子 shell 来执行 script1。 sh,然后另一个子 shell 执行 script2.sh if script1.sh 并且它的子 shell 没有错误退出。完整输出(即来自 ps -ef)将命令显示为 /bin/bash -c "script1.sh && script2.sh"

对于第二个示例,bash 正在运行一个命令,并且本质上是exec 该命令。不需要subshel​​l,script2.sh可以替代成为PID 1。

关于linux - 通过&&运行命令时docker容器中进程的PID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45748034/

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