LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Delete output PIDs (https://www.linuxquestions.org/questions/linux-server-73/delete-output-pids-4175532799/)

preetham.66666 02-02-2015 08:06 AM

Delete output PIDs
 
Hi
lsof +D /root/test/

I want to delete all the PID comes from lsof command output (in below case PID 6356,6986,8184)

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
vi 6356 root 4u REG 8,3 12288 263637 /root/test/.test.txt.swp
vi 6986 root 4u REG 8,3 12288 263793 /root/test/.test1.txt.swp
vi 8184 root 4u REG 8,3 12288 263904 /root/test/test/.test1.txt.swp

Please help me

preetham.66666 02-02-2015 08:20 AM

Hi

I guess below is pretty good

kill -9 `lsof +D /root/test/ -t`

Please share your answers if still there is easiest way

veerain 02-02-2015 11:32 AM

If number of processes to be killed is many then kill command may fail just because command line length overflow.

So this may do the job:

Code:

for pid in `lsof +D /root/test/ -t`; do
kill -9 "$pid"
done


preetham.66666 02-02-2015 09:56 PM

Thanks Veerain

pan64 02-03-2015 12:59 AM

do not use kill -9, but a simple kill (use only -9 if otherwise did not work).
For example in your case kill -9 will obviously kill the process vi, but the file /root/test/.test.txt.swp will not be deleted. Using kill without -9 will allow to delete that file.

veerain 02-03-2015 02:37 AM

What pan64 said is right.

kill -9 used to terminate program immediately.
It does not allow a program to do some cleaning, saving or other tasks a program normally does when exiting.

whereas kill -15 allows a program to do tasks it normally does when exiting gracefully.


All times are GMT -5. The time now is 04:18 AM.