Thursday, October 25, 2007

Terminate batch job (Y/N)? Y

If you happen to use Ruby/Rails on Windows then you've probably got annoyed at least a few times by the "Terminate batch job" prompt you get when typing Control-C at an irb or mongrel session. Even though the program already terminated after a Control-C, the prompt asks anyway and you are forced to give an answer. That's because those Ruby programs are wrapped inside batch files, which have that behaviour hard-coded into cmd.exe.

It's possible to patch cmd.exe, but if your only problem is with some Ruby scripts, you can simply get rid of the batch files and replace then by pure Ruby scripts.

In the case of irb rename bin\irb.bat to bin\irb.rb and remove the lines from the beginning of the file until goto endofruby. For mongrel_rails delete bin\mongrel_rails.cmd and rename bin\mongrel_rails to bin\mongrel_rails.rb. The steps are similar for other files. Sometimes the Ruby script is coded directly inside the batch file. Other times the batch file just invokes Ruby with another file without an extension. But be sure .RB is appended to the PATHEXT environment variable.

The solution is so simple and yet I took months to finally put it in practice...


Roger Pack said...

so I assume you associated .rb files with the ruby interpreter for this?

Romulo said...

Yes, that's required, but I think the Ruby installer already does that by default.

Science said...

Yes indeed - thanks for the tip. For rake, the steps to fix are (in ruby/bin):

1) rename "rake.bat" to "rake.bat.orig" (or whatever)
2) rename "rake" to "rake.rb"

Евгений said...

Another solution: press Ctrl+C twice. Latter one will terminate the cmd session.