LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-07-2016, 04:39 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Rep: Reputation: 34
find and kill process through lsof marked as deleted


Hi can any one help me to write a script, to find and kill process that own a file marked as deleted in lsof and file size is greater than 2GB

Thanks and Regards
 
Old 06-07-2016, 05:07 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by procfs View Post
Hi can any one help me to write a script, to find and kill process that own a file marked as deleted in lsof and file size is greater than 2GB
Look at what plain "lsof" gives you. You get the PID in the second column/field. Then you get the size in the seventh field/column and at the end of the line the indicator "(deleted)". So you could pipe the output of "lsof" into "awk" and get that number and then kill it with "kill" Just be sure to account for the possibility of spaces in file names and paths.
 
Old 06-07-2016, 06:54 AM   #3
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Original Poster
Rep: Reputation: 34
Hi Turbocapitalist, thanks for the update, and we came up with below

lsof -a +L1 | awk -F" " '$8 > 1073741824 {print $2}' | sed 's\^\kill -9 \' > /tmp/kill_script

then we run /tmp/kill_script

Thanks and Regards
 
Old 06-07-2016, 07:06 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Nice. Are you sure that column 8 is the size? On Ubuntu, I have column 7 as the size. Also you can skip the extra step of making a file with "xargs"

Code:
lsof -a +L1 | awk '$7 > 1073741824 && NR >1 {print $2}' | sort -nu | xargs kill
 
Old 06-07-2016, 08:25 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Depending on the process "kill -9" will not allow it to gracefully exit. If other files are open it will lose buffers you just might want to keep. Use -3 instead.

If the process maintains its own log and the process must be restarted, you might try -1 instead. For many daemon services this is a signal to close logs and reopen them, and without losing any active connections.

This is how a log rotate procedure works.
 
Old 06-07-2016, 11:26 PM   #6
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Original Poster
Rep: Reputation: 34
Thanks Jpollard!

Thanks all and Best Regards
 
  


Reply



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
Need Help to find the process running by particular user and kill process one by one LittleMaster Linux - Newbie 1 02-09-2013 02:03 PM
RHEL3 swap partition marked deleted pbaumgar Red Hat 3 09-19-2009 11:25 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
LXer: Recover Deleted Linux Files With lsof LXer Syndicated Linux News 0 06-05-2009 07:51 PM
Looking for a way to find a PID and kill the process it belongs to johngreenwood Programming 6 01-14-2007 05:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:26 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