Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long-running shell commands from Rake that can be interrupted gracefully?

Tags:

ruby

rake

In several projects, I'd like to have a rake task like rake server which will start serving that application via whatever means it needs. Here's one example:

task :server do
  %x{bundle exec rackup -p 1234}
end

This works, but when I'm ready to stop it, pressing Ctrl+c does not shut down gracefully; it interrupts the Rake task itself, which says rake aborted! and gives a stack trace. In some cases I have to do Ctrl+c twice.

I could probably write something with Signal.trap that would interrupt this more gracefully. Is there an easier way?

like image 556
Nathan Long Avatar asked Jan 15 '14 19:01

Nathan Long


1 Answers

trap('SIGINT') { puts "Your message"; exit }

That should make the trick.

You can even add the trap at the task level.

task :server do
  trap('SIGINT') { puts "Your message"; exit }
  %x{bundle exec rackup -p 1234}
end
like image 177
robertodecurnex Avatar answered Sep 18 '22 04:09

robertodecurnex