exception handling - Best way to differentiate between signals for a command-line Ruby script -


i have command-line ruby script need differentiate between different signalexceptions. want catch , ignore control+c (sigint) , exit script when receives control+\ (sigquit).

i'm still relatively new ruby , know best (or canonical) way achieve goal. i'm wondering if there possibility of running os differences (i'm running on os x solution work on windows, linux, etc.?)

i able obtain list of possible signals in 2 different ways:

  1. from bash: stty -a (this nice because displays control key combos)
  2. via irb: signal.list

here how playing around stuff:

#!/usr/bin/env ruby  while true    begin      puts "spinning wheels..."     loop {}    rescue interrupt => e      puts "\ncaught ^c - ignoring"    rescue signalexception => e      puts "\ncaught #{e.inspect}"      if e.message == "sigquit"       puts "shutting down..."       exit     end    end  end 

the correct way capture signals signal.trap:

signal.trap('quit')   puts "shutting down..."   exit end  signal.trap('int', 'ignore') 

the linked docs have comments on os differences, specifically:

the list of available signal names , interpretation system dependent. signal delivery semantics may vary between systems; in particular signal delivery may not reliable.


Comments