February 11, 2007

Posted by John

Tagged mac, mongrel, and terminal

Older: I can mount my ssh where?

Newer: The Latest in Rails

Oops, I did it again

The following post is pretty much just for *nix and mac users. One of the things I did most often when I first started using mongrel for development was start some app up, forget which one and close my terminal window. Now the process was still running so I would get the following error when I tried to fire up my next app on the same port:

Address already in use - bind(2) (Errno::EADDRINUSE)

Being new to the command line, the only way I figured out how to stop it was restart my computer. So uncool, I know. Knowing that this is something newbs run into, I thought I would post a way to fix your problem without restarting.

Let’s say you start up your app like so:

$ mongrel_rails start -d

The -d detaches the executed command from the current terminal but you can still find it in your processes list by typing:

$ ps aux

That will print out all of the currently running processes along with their PID (process identification number). On my system it spits out a big list so a simple trick to find the running mongrel process is:

$ ps aux | grep mongrel

The pipe (|) passes the output of ps aux into the grep search for mongrel. Botta bing bang boom. You now have the process id number (and can do a simple kill to stop it).

PID's

If the PID was 933, you could:

kill 933

You can now search for mongrel in the processes again and it should be gone. Note: If you used script/server to start mongrel, you’ll have to grep that instead of mongrel:

ps aux | grep script/server

10 Comments

  1. Note that your second PID is actually your grep process (now defunct), because it had mongrel as an argument.

    For me killall ruby usually does the trick though.

  2. If you don’t want the grep process to show up I like the nifty trick of taking advantage of how regular expressions are parsed:

    ps aux | grep [m]ongrel

    Alternatively you can do the whole thing (including killing the process) in one go:

    ps aux | grep [m]ongrel | awk '{ print $2 }' | xargs kill -9

    You could extract this into a batch file:

    #!/bin/sh ps aux | grep "$1" | grep -v grep | awk '{ print $2 }' | xargs kill -9

    This could be run as follows to kill any mongrel processes:

    $ ./mykill mongrel

  3. Hmm, silly textile hasn’t parsed that batch file code correctly. Ignore the at-symbols and putting a return in before ps aux (should be two lines).

  4. Gareth Howell Gareth Howell

    Feb 12, 2007

    Why not just run mongel in a foreground window?

    I have screen installed on my Mac, which allows you to spawn multiple terminal sessions in a single window. I run mongel in one of them, irb in another, and the normal console in yet another.

    Gareth

  5. Michael Schuerig Michael Schuerig

    Feb 12, 2007

    Here’s what I’m using. The grep doesn’t list itself in the process list; save in /usr/local/bin/psgrep

    #! /bin/sh
    ps auxww | sed -n -e "1p; /sed -n -e/D; /psgrep/D; /$1/p"

  6. I’m using this command to bump the Mongrel process I detached with script/server:

    
    alias bump='kill -USR2 `cat tmp/pids/mongrel.pid`'
    

    You have to be in your Rails app root to do this, though.

  7. N. Hao Ching N. Hao Ching

    Feb 12, 2007

    Why don’t you just use what the documentation says?

    mongrel_rails stop

    You can also use Eclipse and RadRails to manage your mongrel development servers.

  8. @evan – good point. i should have mentioned that.

    @Gareth – That is what I usually do.

    @N. Hao – This article is for when you detach the mongrel_rails start command from the terminal. I know that mongrel_rails stop works just fine.

  9. Sean Lynch Sean Lynch

    Feb 12, 2007

    You can also use the command “top” on most *nix systems.

    It keeps a list of running processes that can be sorted by cpu, memory or other parameters. They can be for the whole system or for one user id.

    You can kill the job by PID from within top.

    Top is a very useful tool when running *nix.

    http://unixhelp.ed.ac.uk/CGI/man-cgi?top

    It should work on macs:
    http://www.apple.com/macosx/features/unix/

    “Command-line Environment
    UNIX users will feel at home in Darwin, the robust BSD environment that underlies Mac OS X. That environment is accessible at any time from the Terminal application. You can also run commands that don’t require arguments (such as top) by double-clicking them in the Finder. With the thousands of man pages included in Mac OS X, you can quickly find all your favorite UNIX tools.”

  10. You can’t killall if you’re having several ruby processes on the same server. The way I handle such things is to lookup network connections thanks to the netstat command :

    
    netstat -pltn 2>/dev/null | grep THE_PORT_IN_USE 
    
    

    It will give you the process number of the server that use the port.

    Note : If you have several ruby processes on several network interfaces with the same port number (read big server/farm) you may end up with lines. (netstat is interessing : try netstat -alptuv).

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Ruby. More about John.

Syndication

Feed IconRailsTips Articles - An assortment of howto's and thoughts on Ruby and Rails.

Feed IconRails Quick Tips - Ruby and Rails related links.