LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-02-2015, 08:06 AM   #1
preetham.66666
Member
 
Registered: Jan 2015
Posts: 33

Rep: Reputation: Disabled
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
 
Old 02-02-2015, 08:20 AM   #2
preetham.66666
Member
 
Registered: Jan 2015
Posts: 33

Original Poster
Rep: Reputation: Disabled
Hi

I guess below is pretty good

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

Please share your answers if still there is easiest way
 
Old 02-02-2015, 11:32 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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
 
1 members found this post helpful.
Old 02-02-2015, 09:56 PM   #4
preetham.66666
Member
 
Registered: Jan 2015
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thanks Veerain
 
Old 02-03-2015, 12:59 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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.
 
1 members found this post helpful.
Old 02-03-2015, 02:37 AM   #6
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
  


Reply

Tags
command substitution, linux, programing, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] I specifically want to delete my grep output string from the text file tropical781 Linux - Newbie 5 10-09-2012 03:48 PM
switching pids mreff555 Linux - Newbie 4 07-16-2012 03:29 PM
Unable to delete files in /tmp/kdecache : Input/output error wurzeld Linux - Newbie 2 10-28-2008 02:24 PM
G++ environment variables, rerouting output, file listings and PIDs Kawahee Programming 7 09-19-2005 12:40 PM
C++ and PIDs dbcoder Programming 1 07-15-2004 08:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration