Getting at a Childprocess’s pid

I have recently been working with the aruba Cucumber extension to test a command line application. I noticed that aruba is using childprocess to create and control the spawned application; since I needed to do some of this myself, I started using childprocess as well.

I immediately ran into some trouble – I wanted to send a signal to my child process, so I needed the pid of the process. childprocess does know the child’s pid (well, on Windows and Unix, at least), but doesn’t make it public. instance_variable_get solves that problem, but it’s gross. So I made the pid ivar accessible through an attr_reader.

Initially I only implemented the solution for Unix and Windows, since the Java Process-related classes don’t provide any public methods for getting at the new process’s pid. Then jarib, the childprocess gem’s author, pointed me at a Stack Overflow post describing a reflection-based, encapsulating-breaking solution for getting at the pid of a process in Java running on Unix. While I’m never wild about ignoring encapsulation (my entire motivation for making a public reader method…), the reflection-based solution meant that the pid reader method is available on most platforms. The only platforms left out are non-Unix based Java implementations.

A new version of childprocess with these changes is now out there. If the new pid reader makes at least one person’s work a little easier, then I’ll consider the time I invested in it worth it.