I like to start rails server as a daemon with
rails server -d, because I don’t want a terminal window or a tmux pane just hanging around with the server foreground process.
But each time a new gem is added to the application or some changes are made in some configuration file, it is a pain to manually look for the pid in
tmp/pids/server.pid, kill it and start again the server.
I wished if I could have convenient
rails stop and
rails restart commands to help me in the workflow. And it is very easy to get.
Bellow you’ll find a simple
zsh) function that should be added to your
.zshrc). Once done, you’ll be able to do things like the following:
1 2 3 4 5 6 7
Here it is the function:
You see, the code has not too much mystery. Inside the
command is a bash built-in command which executes the given command (in this case
rails), ignoring any shell function named as it (our function). So, we use it to default to original rails executable when the first argument is not
restart, passing any other argument with
If you don’t feel right overwriting
rails, just change the function name and remove the