简体   繁体   English

如何杀死在其上运行的交互式 shell 和进程/作业?

[英]How to kill interactive shell and process/jobs running on it?

I'm using docker container interactive shell to executed the command on the shell.我正在使用 docker 容器交互式 shell 在 shell 上执行命令。

e.g. docker exec -i docker_container_name sh

and I launch commands on this interactive shell.我在这个交互式 shell 上启动命令。 Now If I want to kill the this interactive shell and command running on it, How to do that?现在,如果我想终止此交互式 shell 和在其上运行的命令,该怎么做?

I tried sending kill -9 shell_pid or kill -s 15 shell_pid signal to interactive shell.我尝试向交互式 shell 发送kill -9 shell_pid or kill -s 15 shell_pid信号。 However in this case interactive shell gets killed but the command running on interactive shell is till there and becomes orphan process.然而,在这种情况下,交互式 shell 被杀死,但在交互式 shell 上运行的命令一直存在并成为孤儿进程。

Please let me know how to kill interactive shell with command running on it.请让我知道如何使用运行在其上的命令杀死交互式 shell。

You should get the child(ren) processes first, before killing the shell and kill them too您应该先获取子(ren)进程,然后再杀死外壳并杀死它们

CPIDS=`pgrep -P $shellpid` # gets pids of child processes
kill -9 $shellpid
for cpid in $CPIDS ; do kill -9 $cpid ; done

Or alternatively (this isn't as safe, because, if running series of commands in shell, next one may be called between those two kill commands)或者(这不是那么安全,因为如果在 shell 中运行一系列命令,可能会在这两个 kill 命令之间调用下一个命令)

pkill -TERM -P $shellpid # sends TERMINATE signal to children
kill -9 $shellpid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM