LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-06-2005, 12:19 AM   #1
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Rep: Reputation: 15
shell script to kill processes running on port number


I need a shell script that will find all the processes running on particular port number and then I want to kill those processes

Cmd1: lsof -i : <portnumber> (it will return the process ID)
cmd2: kill -9 <process ID>

plz help
 
Old 12-06-2005, 02:50 AM   #2
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally Posted by varunbihani
Cmd1: lsof -i : <portnumber> (it will return the process ID)
cmd2: kill -9 <process ID>
Code:
kill `lsof -i :80 | tail -n +2 | sed -e 's,[ \t][ \t]*, ,g' | cut -f2 -d' '`
You don't really want to kill -9 immediately. Give the program a chance to shut down cleanly.

Quote:
Originally Posted by varunbihani
plz help
Oh, and most people prefer the english word "please", thank you very much.

Greetings,
Benjamin
 
Old 12-06-2005, 02:52 AM   #3
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Original Poster
Rep: Reputation: 15
thanks but explain

Thanks A lot for the script.. Can you also explain this command??
 
Old 12-06-2005, 03:10 AM   #4
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally Posted by blindcoder
Code:
kill `lsof -i :80 | tail -n +2 | sed -e 's,[ \t][ \t]*, ,g' | cut -f2 -d' '`
Everything between two backticks (`) will be replaced by the output of the command between them.
In this case, this will be executed first:
Code:
lsof -i :80 | tail -n +2 | sed -e 's,[ \t][ \t]*, ,g' | cut -f2 -d' '
So first "lsof -i :80" will be run. Its output will be piped to tail -n +2 which cuts off the first line, which we don't need. The rest will be piped to sed which in this case will truncate all spaces/tabs to one space only for easier parsing. This text is then piped to cut which in this case will print only the second column delimited by a single space (which we created using sed a step earlier). The output will then be all the PIDs without the rest.
Since all this happens within backticks (`) the output from cut will then be inserted in the first command which will then look like this (for example):
Code:
kill 12435 8923 21349
Greetings,
Benjamin.
 
Old 12-06-2005, 03:12 AM   #5
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
man lsof
man sed
man tail
man cut
google linux pipe
Then if you have a question I am sure people will help further
 
Old 12-06-2005, 03:17 AM   #6
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally Posted by Emmanuel_uk
man lsof
man sed
man tail
man cut
google linux pipe
Then if you have a question I am sure people will help further
*sigh* there goes the oh-so-helpful linux community down the drain...
Posts like yours are the reason why I often hear statements along these lines:
The Linux community isn't helpful at all!
All they tell you is to use google! I can tell that myself.

And others like that. Please, if you don't have anything productive to say, just shut the fuck up, thanks.

Benjamin
 
Old 12-06-2005, 03:20 AM   #7
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
not meant to be offensive. Just see my other 500 posts.
Was going to help if question was clearer
Also missed your answer when I posted
By posting I registered on that thread
Do not be so assuming, and mind your language
 
Old 12-06-2005, 03:24 AM   #8
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
whoo, you have more posts than me, I bow before you.
I fail to see where the question was unclear, but maybe that's just me.

Greetings,
Benjamin
 
Old 12-06-2005, 03:35 AM   #9
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
Listen, I have jsut made an appology, you could just please do the same.
No need to be sarcky ok?
If you had checked my posts to see what kind of help I give, you
would not have posted what you just did.

>>Can you also explain this command??
Was not sure what the guy did not understand and where he needed help

Also I explained that I did not see your post. I know I am daft

Ok, peace
 
Old 12-06-2005, 03:35 AM   #10
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
[double posted same text happy clicking problem] deleted

Last edited by Emmanuel_uk; 12-06-2005 at 03:36 AM.
 
Old 12-06-2005, 04:50 AM   #11
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Sorry for it all, but I really get sick at "use google to find out" posts because I keep seeing them literally everywhere I go. So called "help" channels on IRC, mailing lists, newsgroups, everywhere.

I'm sorry to have it taken out on you.

Greetings,
Benjamin
 
Old 12-06-2005, 08:57 AM   #12
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
Thanks. I know, sorry here as well. I was wondering when I was posting about my googling advice being "sane", but I had posted quite a few hints/help to people already, and was running out of steam.
Sometimes script questions are from students, so I tend to want to know what is unclear or expect some homework first.
plz is a good indication of studenthood / younger people. I do not say we have one here, though.

At the same time you had given a great one liner answer
for a *not* such a simple question (IMHO) where one would have expected whoever posted the question to be able to understand / search to understand, not fishing for information / help in that way

Anyhow, I am waffling. This is on top of what I wrote before.

I know googling is not always a suitable message.
Maybe you are kinder, and I am more cynical ?

Nevermind, this is all settled. You know where I come from, and I understand your googlite frustration with non helpful comments
 
Old 12-06-2005, 09:41 AM   #13
dbogdan
Member
 
Registered: Feb 2005
Location: Metro Detroit, US
Distribution: Suse/Slackware/Mepis
Posts: 174

Rep: Reputation: 31
Quote:
Originally Posted by blindcoder
Everything between two backticks (`) will be replaced by the output of the command between them.
In this case, this will be executed first:
Code:
lsof -i :80 | tail -n +2 | sed -e 's,[ \t][ \t]*, ,g' | cut -f2 -d' '
So first "lsof -i :80" will be run. Its output will be piped to tail -n +2 which cuts off the first line, which we don't need. The rest will be piped to sed which in this case will truncate all spaces/tabs to one space only for easier parsing. This text is then piped to cut which in this case will print only the second column delimited by a single space (which we created using sed a step earlier). The output will then be all the PIDs without the rest.
Since all this happens within backticks (`) the output from cut will then be inserted in the first command which will then look like this (for example):
Code:
kill 12435 8923 21349
Greetings,
Benjamin.

Wow! Now that is a truely helpful post. I've been looking to do something similar to this but was kinda stumped... this detailed breakdown made a little light in my head go BING!... and saved me potentially hours of time searching Google and man pages.

Dave
 
Old 12-06-2005, 09:46 AM   #14
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606

Rep: Reputation: 53
something good out of this discussion then
the one liner gets real credit, which is due
 
  


Reply


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
script/command to kill processes running on particular port varunbihani Linux - General 2 07-08-2005 04:35 AM
shell script to kill all processes on specified port varunbihani Linux - General 1 04-19-2005 06:39 AM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 03:09 PM
Starting processes from remote shell and keep them running... johannes Linux - Networking 5 07-23-2003 10:38 AM
how to change number of running processes rsc Linux - Newbie 2 03-14-2002 06:23 PM

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

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