How to kill a for for linux loop?

I am working on Linux, I ran a 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 containing a large number of files that I open in the vim editor one by one. Now I have to skip opening other files between them (i.e. I need to break the for loop). Any suggestion on how to get the PID for the loop? and kill the same. Thanks in advance.

+6
source share
4 answers

Do you want to kill the mission . Press Ctrl Z. Bash will display something like:

 [1]+ Stopped vim $i 

Use this number with kill to send the kill signal:

 kill -9 %1 

and he must be killed afterwards:

 [1]+ Killed vim $i 
+9
source

It can also do the trick:

 while true ; do killall vim ; done 

You can abort this with ^ C, as usual.

+1
source

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

+1
source

To stop creating vim processes, you must stop the for loop. This loop is controlled by the bash process. Therefore, find the PID of the process and kill it. To do this, find the PPID of the vim process, which is the PID of its parent process, and end the parent process: ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | xargs kill 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 make sure that vim instances are not left: ps -elf | tr -s ' ' | grep vim | cut -f4 -d ' ' | xargs kill ps -elf | tr -s ' ' | grep vim | cut -f4 -d ' ' | xargs kill

I see that you are having a "resolved permission" problem. This may be caused by a lack of permission, so to be sure that it will kill processes, you can invoke xargs kill with sudo: ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | sudo xargs kill ps -elf | tr -s ' ' | grep vim | cut -f5 -d ' ' | sudo xargs kill

0
source

Source: https://habr.com/ru/post/915069/


All Articles