How to Use Ctrl+C to Restart a Process in a Loop

I start my Rails development processes by using AppleScript to set up iTerm2. I often need to restart my Rails processes when I change something in config/initializers, or if I add a new gem to the Gemfile.

I wanted to create a “restart loop” so that pressing Ctrl+C would stop the process and then restart it. I also wanted to have the option to break out of the loop and get back to the bash prompt.

The solution was to use the trap command to ignore the INT signal from Ctrl+C. I also needed to add a short delay before restarting the process, so that I could exit the loop by pressing Ctrl+C twice.

Here’s the command I’m running to start the Rails server with a restart loop:

(trap true INT; while sleep 0.5; do echo 'Starting Rails...'; \
  rails server --binding=127.0.0.1 --port 3000; done)

In my .dev.scpt script, I’m now using a subroutine to wrap all the commands:

on commandWithRestartLoop(label, command)
  return "(trap true INT; while sleep 0.5; do " & ¬
    "echo 'Starting " & label & "...'; " & ¬
    command & "; done)"
end commandWithRestartLoop
# ...
write text my commandWithRestartLoop("Rails", ¬
  "rails server --binding=127.0.0.1 --port 3000")
# ...
write text my commandWithRestartLoop("Sidekiq", "sidekiq")