Search This Blog

June 22, 2011

Kill process listening/bound to specific port

Step 1: Identify the process
    For this you can use either of the command below:
lsof -w -n -i tcp:<port_number>
-w implies suppression of warning messages
-n option inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host name lookup is not working properly.
-i[i] option selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.
      If -i4 or -i6 is specified with no following address, only files of the indicated IP version, IPv4 or IPv6, are displayed. (An IPv6 specification may be used only if the dialects supports IPv6, as indicated by "[46]" and "IPv[46]" in lsof's -h or -? output.) Sequentially specifying -i4, followed by -i6 is the same as specifying -i, and vice-versa. Specifying -i4, or -i6 after -i is the same as specifying -i4 or -i6 by itself.
    Multiple addresses (up to a limit of 100) may be specified with multiple -i options. (A port number or service name range is counted as one address.) They are joined in a single ORed set before participating in AND option selection.
      An Internet address is specified in the form (Items in square brackets are optional.):
            46 specifies the IP version, IPv4 or IPv6 that applies to the following address. '6' may be be specified only if the UNIX dialect supports IPv6.  If neither '4' nor '6' is specified, the following address applies to all IP versions.

            protocol is a protocol name - TCP or UDP.

            hostname is an Internet host name.  Unless a specific IP version is specified, open network files associated with host names of all versions will be selected.

            hostaddr is a numeric Internet IPv4 address in dot form; or an IPv6 numeric address in colon form, enclosed in brackets, if the UNIX dialect supports IPv6.  When an IP version is selected, only its numeric addresses may be specified.

            service is an /etc/services name - e.g., smtp - or a list of them.

            port is a port number, or a list of them.

Sample Command Line Output:
karthik@cloud:~/MyRubyProjects/dashboard$ lsof -w -n -i tcp:3000
ruby    2969 karthik    5u  IPv4  68913      0t0  TCP *:3000 (LISTEN)

Alternatively, you may use the command below:
netstat -anp | grep :<port_number>
-a displays all active connections and the TCP and UDP ports on which the computer is listening.
-n displays active TCP connections, however, addresses and port numbers are expressed numerically and no attempt is made to determine names.
-p Linux: Process : Show which processes are using which sockets (similar to -b under Windows) (you must be root to do this)

Sample Command Line Output:
karthik@cloud:~/MyRubyProjects/dashboard$ netstat -anp | grep :3000
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp        0      0  *               LISTEN      2969/ruby    

Step 2: Kill the process

kill -9 <procecss_id_number>

karthik@cloud:~/MyRubyProjects/dashboard$ kill -9 2969

June 20, 2011

When you see Gem error like - "'': uninitialized constant Gem::SilentUI (NameError)"

I just created a new Rails 3 project called dashboard and tried having bundler install all the required basic gems for this project using the command - bundle install. Damn!, I got the error that is the title for this blog post.
Below is the snap shot of the command and the error:

karthik@cloud:~/MyRubyProjects/beach_projects/dashboard$ bundle install
/home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:56:in '<class:UI>': uninitialized constant Gem::SilentUI (NameError)
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:2:in `<module:Bundler>'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/ui.rb:1:in `<top (required)>'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/cli.rb:16:in `initialize'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `new'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor.rb:246:in `dispatch'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/lib/bundler/vendor/thor/base.rb:389:in `start'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/gems/bundler-1.0.7/bin/bundle:13:in `<top (required)>'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/bin/bundle:19:in `load'
from /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3/bin/bundle:19:in `<main>'

What is the issue? The issue is perhaps with the version of bundler and its dependencies. To resolve this issue, I ran the gem update for the bundler and know what? It worked ;)
Below is the snap shot of the command and the output:

karthik@cloud:~/MyRubyProjects/beach_projects/dashboard$ gem update bundler
Updating installed gems
Updating bundler
Fetching: bundler-1.0.15.gem (100%)
Successfully installed bundler-1.0.15
Gems updated: bundler

After this I tried running the "bundle install" command and the required gems were installed successfully.
Below is the snap shot of the command and the output:

karthik@cloud:~/MyRubyProjects/beach_projects/dashboard$ bundle install
Fetching source index for
Installing rake (0.9.2) 
Using abstract (1.0.0) 
Using activesupport (3.0.3) 
Using builder (2.1.2) 
Installing i18n (0.6.0) 
Using activemodel (3.0.3) 
Using erubis (2.6.6) 
Installing rack (1.2.3) 
Installing rack-mount (0.6.14) 
Installing rack-test (0.5.7) 
Installing tzinfo (0.3.28) 
Using actionpack (3.0.3) 
Using mime-types (1.16) 
Using polyglot (0.3.1) 
Using treetop (1.4.9) 
Installing mail (2.2.19) 
Using actionmailer (3.0.3) 
Installing arel (2.0.10) 
Using activerecord (3.0.3) 
Using activeresource (3.0.3) 
Using bundler (1.0.15) 
Using thor (0.14.6) 
Using railties (3.0.3) 
Using rails (3.0.3) 
Using sqlite3 (1.3.3) 
Using sqlite3-ruby (1.3.3)
Your bundle is complete! It was installed into /home/karthik/.rvm/gems/ruby-1.9.2-p0@rails3


Happy bundling :P

June 17, 2011

How to find the method definition - one that is invoked at runtime?

During the QnA session after my talk at RubyConf India 2011, one avid Ruby enthusiast asked me a question, "How to find the method definition - one that is invoked at runtime?".

I confessed that in all my experience (for the last eight and odd months) with Ruby, I have been doing the grep-ing for the method name and then figure out which method definition would be the last to over-ride every other ones declared earlier. No sooner did I finish answering, boooooooooom came a greatly handy tip from a very respectable and renowned geeky person in the crowd - Ola Bini. He adviced to use Ruby's "method" method to figure out the source of the method that was invoked at runtime. What an easier and neater approach!

Hearty thanks to both the folks - one who questioned and the other who answered.

This blog post is my attempt to elaborate more on the tip that Ola Bini had adviced.

Back home after the conference, I googled on these lines and found the following resources useful (in fact, this blog post is only my assimilation of these). May you find them useful.

Additional resources of interest:

June 7, 2011

Ruby Conf India 2011

I happened to be one of the speakers in this international conference that was held at Bangalore. What pleasure and privilege! The subject of my talk was Deciphering the Ruby Object Model, aimed to help fellow ruby enthusiasts level up their understanding and appreciation of Ruby as a programming language. The talk was well received by many a folks who had attended my session. I wish to thank every one who had personally come over to me for sharing their feedback with me.

You can download the presentation from any of the below mentioned locations:
Once the conference videos are published, I'll be glad to add its link here ;) Okie, it is now published, so please find it below:

Deciphering The Ruby Object Model from Innovation & Technology Trust on Vimeo.
For all the positive feedback I had received, I intend to very soon publish a couple of blog posts detailing on specific areas of interest. If you have attended my talk and wish I write up a blog post or two on specific areas please do feel free to communicate it via comments to this blog post and I shall make my humble and sincere attempts to dispell some of the confusion and enlighten you in that area.

Any other feedback that you would like to share is also welcome ;) You may always use the comments section of this blog for this purpose.

A few sites or blog posts that did have a mention of my talk or appreciations/criticism for my talk are put listed below. They'll be my motivation to move forward - further and faster!

Some live tweets while I was delivering my talk can be found below:

As I have mentioned in my talk, the following books should be a part of your reading book-shelf if you are serious about programming in Ruby: