如何在 linux 上终止正在运行的 for 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10479457/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:12:13  来源:igfitidea点击:

How to kill a running for loop on linux?

linuxpid

提问by BSalunke

I'm working on Linux, I have executed the for loop on a Linux terminal as follows:

我在 Linux 上工作,我在 Linux 终端上执行了 for 循环,如下所示:

for i in `cat fileName.txt`
do
echo $i
vim $i
done

fileName.txt is a file contains the large no of file entries that I'm opening in vim editor one by one. Now i have to skip opening other files in between.(i.e. i have to break the for loop). Any suggestion how to get the PID of running for loop? and kill the same. Thanks in advance.

fileName.txt 是一个文件,其中包含我在 vim 编辑器中一一打开的大量文件条目。现在我必须跳过在两者之间打开其他文件。(即我必须打破 for 循环)。任何建议如何获得运行循环的PID?并杀死相同。提前致谢。

回答by Thomas

This might also do the trick:

这也可以解决问题:

while true ; do killall vim ; done

You can abort this one with ^C as usual.

你可以像往常一样用 ^C 中止这个。

回答by eepp

You want to kill the running job. Press CtrlZ. Bash will show something like:

您想终止正在运行的作业。按CtrlZ。Bash 将显示如下内容:

[1]+  Stopped                 vim $i

Use that number with killto send the KILLsignal:

使用该号码kill发送KILL信号:

kill -9 %1

and it should be killed afterwards:

它应该在之后被杀死:

[1]+  Killed                  vim $i

回答by sneak

Find the process id (PID) of the parent of the vim process, which should be the shell executing the for loop. Try using "pstree -p". Then, kill that process id using "kill ".

找到 vim 进程的父进程的进程 id (PID),它应该是执行 for 循环的 shell。尝试使用“pstree -p”。然后,使用“kill”杀死该进程ID。

回答by chab42

To terminate the creation of the vim processes, you must stop the for loop. This loop is managed by a bash process. So find the PID of the process and kill it. To do so, find the PPID of a vim process, which is the PID of its parent process, and terminate the parent process : ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | xargs kill

要终止 vim 进程的创建,您必须停止 for 循环。该循环由 bash 进程管理。所以找到进程的PID并杀死它。为此,找到 vim 进程的 PPID,即其父进程的 PID,并终止父进程: ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | xargs kill

You can use this command with this second one, which will kill all vim processes, to ensure no vim instances remain: ps -elf | tr -s ' ' | grep vim | cut -f4 -d ' ' | xargs kill

您可以将此命令与第二个命令一起使用,这将终止所有 vim 进程,以确保没有 vim 实例保留: ps -elf | tr -s ' ' | grep vim | cut -f4 -d ' ' | xargs kill

I see that you had a problem with "permission not permitted". This might be caused by a lack of permission, so to be sure it will kill the processes, you can call xargs kill with sudo : ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | sudo xargs kill

我看到您遇到了“权限不允许”的问题。这可能是由于缺乏权限造成的,因此为了确保它会杀死进程,您可以使用 sudo 调用 xargs kill :ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | sudo xargs kill