2
  trap 'TERM' do
    warn 'Exiting.'
    exit 1
  end

This prints a 10 line stacktrace.

How to suppress the stacktrace and exit silently?

Ruby 2.2.0

B Seven
  • 44,484
  • 66
  • 240
  • 385

2 Answers2

3

You can do it like this:

trap "TERM" do
  warn "Exiting."
  $stderr.reopen(IO::NULL)
  $stdout.reopen(IO::NULL)
  exit 1
end
sawa
  • 165,429
  • 45
  • 277
  • 381
0

If you hit Ctrl + C, the signal will be INT, not TERM. If you want to catch both, you can do something like this:

p = proc do
  warn 'Exiting.'
  exit 1
end
trap 'INT',p
trap 'TERM',p
Shelvacu
  • 4,245
  • 25
  • 44