Debugging a Ruby daemon

Mon, May 6, 2013 2-minute read

Today I had to develop a Ruby daemon for a project I work on. A daemon is a program that runs as a background process, rather than being under the direct control of an interactive user.

Most Ruby developers know about Pry, a powerful alternative to the standard IRB (interactive ruby) shell. Pry can be invoked on any object using the

my_object.pry
syntax or on the current binding (or any binding) using

binding.pry

The Pry session will then begin within the scope of the object (or binding). When the session ends the program continues with any modifications you made to it.

Needless to say it is a very powerful debugging tool, since it can be invoked in the middle of a running program.

In order to open a Pry session in a background process, pry-remote could be used.

Pry-remote provides a way to start Pry remotely and to connect to it using DRb (distributed object system for Ruby). This allows you to access the state of the running program from anywhere, over the Internet.

Pry-remote has a good documentation and I encourage you to read through it. A general use-case for me is to call it via

binding.remote_pry
which then prompts a message in the log, similar to:

[pry-remote] Waiting for client on drb://localhost:9876

Then we connect to drb using

pry-remote
and use Pry.

Resources