This is the script I’m using when I start my local Rails development server. It will automatically detect the next available port starting at the default port 3000 so I can have multiple servers running without remembering what ports that are already busy.

#!/usr/bin/env ruby

DEFAULT_PORT = 3000

puts (`ps aux | grep [r]uby | grep script/server`.split("\n").map { |l|<br />
  server = l.split(/\s+/)[11..-1]<br />
  (port = server.index('-p')) ? server[port.next].to_i : DEFAULT_PORT<br />
}.max || DEFAULT_PORT - 1).next

I aliased it to ss in .bash_profile like this:

alias ss='./script/server -p `~/bin/available_rails_port`'

(Btw, Happy 2008! I haven’t updated this site for a while)