bash Docker 杀死容器内的进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46910412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker kill process inside container
提问by Maksim Luzik
I exec into Docker container with docker exec -it container-name bash
我执行到 Docker 容器 docker exec -it container-name bash
Inside container I run command ps aux | grep processName
在容器内我运行命令 ps aux | grep processName
I receive a PID and after that I run:
我收到一个 PID,然后我运行:
kill processId
but receive:
kill processId
但收到:
-bash: kill: (21456) - No such process
-bash: kill: (21456) - No such process
Am I missing something or? I know that Docker shows different process IDs from top
command inside the host and ps aux
inside the container (How to kill process inside container? Docker top command), but I am running this from inside container?
我错过了什么吗?我知道 Docker 从top
主机ps aux
内部和容器内部的命令中显示不同的进程 ID (How to kill process inside container? Docker top command),但我是从容器内部运行这个?
回答by OscarAkaElvis
That response is because the process you are trying to kill is not existing at the moment of killing it. For example, if you launch ps aux
you can get an output like this inside a container (it depends of the container of course):
该响应是因为您试图杀死的进程在杀死它时不存在。例如,如果您启动,ps aux
您可以在容器内获得这样的输出(当然这取决于容器):
oot@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 15 0.0 0.0 36840 2904 pts/0 R+ 13:57 0:00 ps aux
Then if you try to kill process with PID 15 you'll get the error because PID 15 is finished at the moment of trying to kill it. The ps command terminates after showing you the processes info. So:
然后,如果您尝试使用 PID 15 终止进程,则会收到错误消息,因为 PID 15 在尝试终止它时已完成。ps 命令在向您显示进程信息后终止。所以:
root@69fbbc0ff80d:/# kill 15
bash: kill: (15) - No such process
In a docker container you can kill process in the same way as normal excepting the root process (id 1). You can't kill it:
在 docker 容器中,除了根进程(id 1)之外,您可以按照与正常相同的方式终止进程。你不能杀死它:
root@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 16 0.0 0.0 36840 2952 pts/0 R+ 13:59 0:00 ps aux
root@69fbbc0ff80d:/# kill 1
root@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 17 0.0 0.0 36840 2916 pts/0 R+ 13:59 0:00 ps aux
As you can see you can't kill it. Anyway if you want to proof that you can kill processes you can do:
如您所见,您无法杀死它。无论如何,如果你想证明你可以杀死进程,你可以这样做:
root@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 18 0.0 0.0 36840 3064 pts/0 R+ 14:01 0:00 ps aux
root@69fbbc0ff80d:/# sleep 1000 &
[1] 19
root@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 19 0.0 0.0 4372 724 pts/0 S 14:01 0:00 sleep 1000
root 20 0.0 0.0 36840 3016 pts/0 R+ 14:01 0:00 ps aux
root@69fbbc0ff80d:/# kill 19
root@69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 21 0.0 0.0 36840 2824 pts/0 R+ 14:01 0:00 ps aux
[1]+ Terminated sleep 1000
Hope it helps.
希望能帮助到你。