Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2022 04:56
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save keithrbennett/d95653af91184b9eb9b35d26f8b9b2e8 to your computer and use it in GitHub Desktop.
Illustrates the use of 'trap' in Ruby for crude interprocess communication
#!/usr/bin/env ruby
# Author: @keithrbennett (Github)
# Illustrates how to respond to Unix signals in a Ruby program, using SIGUSR1 and SIGUSR2
# for user-defined signals, and SIGINT for trapping Ctrl-C.
require 'awesome_print'
require 'json'
require 'yaml'
$stderr.puts <<~GREETING
Pid is #{}.
Standard output is a #{$stdout.tty? ? 'tty' : 'block'} device.
You can send signals SIGUSR1 and SIGUSR2 (e.g. via `kill`) to poke this program
to output its stats in human readable or YAML forms:
kill -SIGUSR1 #{} # Human readable (Awesome/Amazing Print)
kill -SIGUSR2 #{} # YAML
...and to terminate the program, outputting the final data observation:
kill -SIGINT #{} # Terminate
SystemInfo =, :uptime, :res_memory_mb)
# See this great article about monotonic time:
def monotonic_time_now
START_TIME = monotonic_time_now
def monotonic_uptime
monotonic_time_now - START_TIME
def resident_memory_in_mb
# -o = format with the following data
# rss = resident memory set size, in ?
# h = omit the header ("RSS")
command = "ps -o rss h #{}"
output = `#{command}`
kb = output.strip.to_i
(kb / 1024.0).round(0)
def system_info, monotonic_uptime.round(2), resident_memory_in_mb)
def print_system_info
ap system_info.to_h
Signal.trap('USR1') { print_system_info }
Signal.trap('USR2') { puts system_info.to_h.to_yaml }
Signal.trap('INT') do
puts "\nEnding program, final stats are:"
# See for signal values.
#Signal.trap(30) { puts 30 }
#Signal.trap(10) { puts 10 }
memory_black_hole = []
loop do
sleep 0.5
memory_black_hole << { }
#puts "Time count: #{ObjectSpace.each_object(Time).count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment