A couple of posts back, I wrote about how you can get status info out of a long running dd process by sending it a signal. The functionally is something you can bring to your own Ruby scripts.

It that post I looked at two signals, SIGINFO which is specifically designed for this functionally and SIGUSR1 exists to allow user defined behaviors. SIGINFO is only present on BSD derived OSs, including OS X. SIGINFO is nicer because it’s bound to a key press, Control-T. However, on Linux SIGINFO isn’t present and there’s not equivalent key binding.

Fortunately, we’re using a dynamic language and can support both signals in the same code.

First we need code to print the status:

1
2
3
def display_status
  puts "\nThe things is doing the thing and has got this far!\n"
end

Informative, eh? Well, I don’t what your program is suppose to say! That’s your problem. The one thing of note here is that I like to start the message with a new line, just to make sure the screen is clear and readable. Once we have way to display the status, we need to activate it:

1
Signal.trap('USR1') { display_status }

As noted, SIGINFO only exists on BSD systems, trying to trap it on Linux will raise an ArgumentError. To conditionally use it:

1
2
Signal.trap('USR1') { display_status }
Signal.trap('INFO') { display_status } if Signal.list.include? 'INFO'

Or, if you only want one signal:

1
2
signal = Signal.list.include?('INFO') ? 'INFO' : 'USR1'
Signal.trap(signal) { display_status }

In either case, your program will respond to ^T on BSD systems and spit out a status.

Now I realize that we’re living in the 21st Century and that most people don’t write command line scripts and more, especially long running ones, but if you do, take 5 minutes and add this feature. It’s simple and your users might actually thank you.

Comments