How to kill a running for loop on linux?
Solution 1
You want to kill the running job. Press CtrlZ. Bash will show something like:
[1]+ Stopped vim $i
Use that number with kill
to send the KILL
signal:
kill -9 %1
and it should be killed afterwards:
[1]+ Killed vim $i
Solution 2
This might also do the trick:
while true ; do killall vim ; done
You can abort this one with ^C as usual.
Solution 3
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 ".
BSalunke
I'm a software developer working on C/C++ programming Linux platform. Also I learning Linux kernel programming.
Updated on July 25, 2022Comments
-
BSalunke almost 2 years
I'm working on Linux, I have executed the for loop on a Linux terminal as follows:
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.