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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-06-2005, 12:19 AM
|
#1
|
Member
Registered: Jul 2003
Location: indore, india
Posts: 69
Rep:
|
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
|
|
|
12-06-2005, 02:50 AM
|
#2
|
ROCK Linux
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108
Rep:
|
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
|
|
|
12-06-2005, 02:52 AM
|
#3
|
Member
Registered: Jul 2003
Location: indore, india
Posts: 69
Original Poster
Rep:
|
thanks but explain
Thanks A lot for the script.. Can you also explain this command??
|
|
|
12-06-2005, 03:10 AM
|
#4
|
ROCK Linux
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108
Rep:
|
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.
|
|
|
12-06-2005, 03:12 AM
|
#5
|
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:
|
man lsof
man sed
man tail
man cut
google linux pipe
Then if you have a question I am sure people will help further
|
|
|
12-06-2005, 03:17 AM
|
#6
|
ROCK Linux
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108
Rep:
|
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
|
|
|
12-06-2005, 03:20 AM
|
#7
|
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:
|
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
|
|
|
12-06-2005, 03:24 AM
|
#8
|
ROCK Linux
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108
Rep:
|
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
|
|
|
12-06-2005, 03:35 AM
|
#9
|
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:
|
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
|
|
|
12-06-2005, 03:35 AM
|
#10
|
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:
|
[double posted same text happy clicking problem] deleted
Last edited by Emmanuel_uk; 12-06-2005 at 03:36 AM.
|
|
|
12-06-2005, 04:50 AM
|
#11
|
ROCK Linux
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108
Rep:
|
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
|
|
|
12-06-2005, 08:57 AM
|
#12
|
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:
|
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
|
|
|
12-06-2005, 09:41 AM
|
#13
|
Member
Registered: Feb 2005
Location: Metro Detroit, US
Distribution: Suse/Slackware/Mepis
Posts: 174
Rep:
|
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
|
|
|
12-06-2005, 09:46 AM
|
#14
|
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:
|
something good out of this discussion then
the one liner gets real credit, which is due
|
|
|
All times are GMT -5. The time now is 11:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|