Howdy, fellow geekster. I'm Robin Curry, and I mostly blog about Ruby, Rails, jQuery, css, development, and web design.


Follow me on twitter or grab my feed.

A better script/server alias for Rails

Posted: August 13th, 2009 | Author: robincurry | Filed under: Uncategorized | Tags: , | 1 Comment »

I’ve long had a bash alias for starting up script/server for a rails process that looked something like:

alias ss="./script/server"

This has worked all well and good, but I’ve been wanting more. I often find myself switching between projects and the time and effort it takes is just exhausting. I mean, think of all those keystrokes!

Inspired by @MikeG1′s tweet pining for a script/server to kill any other process running on same port, and a desire to make the switching process easier for myself, I came up with the following function that I added to my bash profile.

Now switching between rails apps is effortless. I can start the rails app from anywhere with no worries about conflicting processes (bye bye, you’ve been terminated) or even changing to the proper directory. Ahhh, my hands feel lighter already.

Enjoy!


# ss – A better script/server alias.
# Usage:
# ss <my_rails_app>
# Will
# – cd to the project directory (if argument provided, otherwise will use current directory)
# – kill any existing rails server instances
# – start the rails server

function ss {
  if [ "$1" ]; then
    # cd to the rails app directory.
    cd ~/proj/repos;  # (update the repository root location to suit your needs)
    if [ "$1" ]; then
      cd `ls|grep $1|sort|tail -1`
    fi
    echo $(pwd)
  fi
  # kill any existing rails server processes.
  ps -a|grep "ruby ./script/server"|grep -v "grep"|cut -d " " -f1|xargs -n 1 kill -TERM

  # start the server.
  ruby ./script/server
}

Popularity: 63% [?]


One Comment on “A better script/server alias for Rails”

  1. 1 luke hartman said at 11:12 am on August 13th, 2009:

    Thanks. I’ve never enjoyed tracking down the process to kill just to start it up again.