Ruby provides several simple ways to launch subprocesses. Ruby's popen method comes into play in situations where cross platform handling of subprocess IO is required. Ruby also provides mechanisms to trap and send signals, such as the SYSINT (Control-C) signal. Putting popen and signal handling together to launch and manage a subprocess is straight forward but there's a limitation; popen lacks support for capturing standard error. By default standard error output is just displayed on the screen as if the subprocess was launched from the command line. There are a couple of workarounds available, in some cases it may be acceptable to discard standard error by redirecting it as part of the subprocess launch command, i.e. "command 2>/dev/null"; another, and typically better, option is to redirect standard error so that it's merged with standard output, i.e. "command 2>&1". Unfortunately the redirection impacts the Process ID (pid) for the launched subprocess, which breaks the simple standard signal handling that I mentioned earlier. In addition to the pid issue, getting the Control-C signalling support to work in Windows can be challenging. This post walks through an example that shows popen usage and how to send Control-C signals to a subprocess in Windows and Unix when standard error is redirected. It concludes with a brief discussion of popen3 and popen4 as alternatives to popen. Read the rest of this entry