Permalink
Browse files

ensure termination of child dtrace on CLI term by a signal

  • Loading branch information...
1 parent 77b556d commit 4d0f6aeb9611900d7ee34075f921f3a707e8659d @trentm committed Nov 2, 2012
Showing with 20 additions and 1 deletion.
  1. +3 −1 CHANGES.md
  2. +17 −0 bin/bunyan
View
@@ -2,7 +2,9 @@
## bunyan 0.16.1 (not yet released)
-(nothing yet)
+- Ensure that a possible dtrace child process (with using `bunyan -p PID`) is
+ terminated on signal termination of the bunyan CLI (at least for SIGINT,
+ SIGQUIT, SIGTERM, SIGHUP).
## bunyan 0.16.0
View
@@ -65,6 +65,9 @@ Object.keys(levelFromName).forEach(function (name) {
// The current raw input line being processed. Used for `uncaughtException`.
var currLine = null;
+// Child dtrace process, if any. Used for signal-handling.
+var child = null;
+
//---- support functions
@@ -905,6 +908,7 @@ function processPid(opts, stylize, callback) {
var argv = ['dtrace', '-Z', '-x', 'strsize=4k', '-qn',
format('bunyan%d:::log-*{printf("%s", copyinstr(arg0))}', opts.pid)];
var dtrace = spawn(argv[0], argv.slice(1));
+ child = dtrace; // intentionall global
dtrace.stderr.pipe(process.stderr);
@@ -1023,6 +1027,19 @@ function asyncForEach(arr, iterator, callback) {
//---- mainline
+// Try to ensure we close a child 'dtrace' process on signalled exit.
+function signalCleanupAndExit(signal) {
+ if (child) {
+ child.kill(signal);
+ }
+ process.exit();
+}
+process.on('SIGINT', function () { signalCleanupAndExit('SIGINT'); });
+process.on('SIGQUIT', function () { signalCleanupAndExit('SIGQUIT'); });
+process.on('SIGTERM', function () { signalCleanupAndExit('SIGTERM'); });
+process.on('SIGHUP', function () { signalCleanupAndExit('SIGHUP'); });
+
+
process.on('uncaughtException', function (err) {
function indent(s) {
var lines = s.split(/\r?\n/);

0 comments on commit 4d0f6ae

Please sign in to comment.