DEV Community

loading...

Restart your rails server automatically

Kelly Stannard
Developer. Design pattern enthusiast. Tilting at Agile practices.
・1 min read

I have been working on changes to my config files and testing some middleware and it gets tedious trying remembering to stop and restart the server for each change. Enter fswatch to the rescue.

fswatch -o config other/paths | ruby -e '
while (pid=fork{ exec("be rails s") }) && gets do
  Process.kill("TERM", pid); Process.wait
end'
Enter fullscreen mode Exit fullscreen mode

fswatch sends a message into the pipe anytime the files change. The -o flag batches all changes found into a single message so that we aren't restarting excessively.

The ruby process controls the rails subprocess. fork will create a Ruby subprocess and then exec replaces the Ruby process with a Rails subprocess. Any time fswatch sees changes, it will send a string into the pipe and ruby picks it up with gets and sends a TERM signal to Rails to shut it down. Once Rails has shut down, we loop back and start a new Rails subprocess.

Discussion (0)