Skip to content

Instantly share code, notes, and snippets.

@unclebilly
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unclebilly/9629558 to your computer and use it in GitHub Desktop.
Save unclebilly/9629558 to your computer and use it in GitHub Desktop.
Handle the USR1 signal by printing backtraces
#!/usr/bin/env ruby
puts "PID: #{$$}"
trap("USR1") do
@dump = true
end
t = Thread.new do
loop do
print '<'
sleep 1
end
end
f = Thread.new do
loop do
f = rand(10) + rand(10) + rand(10) + rand(10)
sleep 0.003
end
end
printer = Thread.new do
loop do
if @dump
Thread.list.each do |t|
puts t.inspect
puts t.backtrace
end
@dump = false
else
sleep 0.5
end
end
end
printer.join
@runswithd6s
Copy link

Cool! Signal handling FTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment