Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Last active May 25, 2020 11:40
Show Gist options
  • Save lmarburger/ede778f8d6907b7c41d7fcfcd554d2a9 to your computer and use it in GitHub Desktop.
Save lmarburger/ede778f8d6907b7c41d7fcfcd554d2a9 to your computer and use it in GitHub Desktop.
Quit and suspend a ruby process for debugging
require 'bundler/setup'
require 'rbtrace'
require 'English'
$run = true
Signal.trap('HUP') { $run = false }
while $run do
puts "pid=#{$PID} time=#{Time.now}"
sleep 10
end
puts 'done'
$ ruby work.rb
pid=24203 time=2017-03-08 15:10:00 -0500
pid=24203 time=2017-03-08 15:10:10 -0500
done
waiting...
I can debug this process all day
$ rbtrace --pid=24203 --eval='at_exit { $do_not_exit = true; puts "waiting..."; while $do_not_exit do sleep 1 end }'
*** attached to process 24203
>> at_exit { $do_not_exit = true; puts "waiting..."; while $do_not_exit do sleep 1 end }
=> #<Proc:0x007ffe0899cb78@(eval):1>
*** detached from process 24203
$ kill -HUP 24203
$ rbtrace --pid=24203 --eval='puts "I can debug this process all day"'
*** attached to process 24203
>> puts "I can debug this process all day"
=> nil
*** detached from process 24203
$ rbtrace --pid=24203 --eval='$do_not_exit = false'
*** attached to process 24203
*** timed out waiting for eval response
*** process 24203 is gone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment