How can I “intercept” Ctrl+C in a CLI application?

Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); This should be able to intercept the signal, but only as an intermediate step before the JVM completely shutdowns itself, so it may not be what you are looking after. You need to use a SignalHandler (sun.misc.SignalHandler) to intercept the … Read more

Continuously read from STDOUT of external process in Ruby

I’ve had some success in solving this problem of mine. Here are the details, with some explanations, in case anyone having a similar problem finds this page. But if you don’t care for details, here’s the short answer: Use PTY.spawn in the following manner (with your own command of course): require ‘pty’ cmd = “blender … Read more

How do I write to a Python subprocess’ stdin?

It might be better to use communicate: from subprocess import Popen, PIPE, STDOUT p = Popen([‘myapp’], stdout=PIPE, stdin=PIPE, stderr=PIPE) stdout_data = p.communicate(input=”data_to_write”)[0] “Better”, because of this warning: Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

Using fflush(stdin)

Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard: int fflush(FILE *ostream); ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that … Read more