Signal Events

function () {}

Emitted when the processes receives a signal. Here is a list of signals from the OSX sigaction(2) manual page:

     The following is a list of all signals with names as in the include file <signal.h>:

     NAME            Default Action          Description
     SIGHUP          terminate process       terminal line hangup
     SIGINT          terminate process       interrupt program
     SIGQUIT         create core image       quit program
     SIGILL          create core image       illegal instruction
     SIGTRAP         create core image       trace trap
     SIGABRT         create core image       abort(3) call (formerly SIGIOT)
     SIGEMT          create core image       emulate instruction executed
     SIGFPE          create core image       floating-point exception
     SIGKILL         terminate process       kill program
     SIGBUS          create core image       bus error
     SIGSEGV         create core image       segmentation violation
     SIGSYS          create core image       non-existent system call invoked
     SIGPIPE         terminate process       write on a pipe with no reader
     SIGALRM         terminate process       real-time timer expired
     SIGTERM         terminate process       software termination signal
     SIGURG          discard signal          urgent condition present on socket
     SIGSTOP         stop process            stop (cannot be caught or ignored)
     SIGTSTP         stop process            stop signal generated from keyboard
     SIGCONT         discard signal          continue after stop
     SIGCHLD         discard signal          child status has changed
     SIGTTIN         stop process            background read attempted from control terminal
     SIGTTOU         stop process            background write attempted to control terminal
     SIGIO           discard signal          I/O is possible on a descriptor (see fcntl(2))
     SIGXCPU         terminate process       cpu time limit exceeded (see setrlimit(2))
     SIGXFSZ         terminate process       file size limit exceeded (see setrlimit(2))
     SIGVTALRM       terminate process       virtual time alarm (see setitimer(2))
     SIGPROF         terminate process       profiling timer alarm (see setitimer(2))
     SIGWINCH        discard signal          Window size change
     SIGINFO         discard signal          status request from keyboard
     SIGUSR1         terminate process       User defined signal 1
     SIGUSR2         terminate process       User defined signal 2

Example of restarting an HTTP server upon receiving a SIGHUP signal:

var http = require('http');
var server;

function StartServer() {
  console.log("Starting server...");
  // Initalizations such as reading the config file, etc.
  server = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
  });
  server.listen(1337, "127.0.0.1");
}

function StopServer(shutdown) {
  console.log("Stopping server...");
  // Other cleanup functions here
  if(server) {
    server.close();
    if(shutdown) {
      console.log("Exiting server process.");
      process.exit();
    }
  }
}

process.on("SIGHUP", function(){
  StopServer();
  StartServer();
});

StartServer();

An example of this signal handling in progress:

-- server console
$ node restart_server.js 
Starting server...

-- admin console
$ curl http://localhost:1337/
Hello World
$ kill -HUP 1179

-- server console
Stopping server...
Starting server...

-- admin console
$ curl http://localhost:1337/
Hello World

An easy way to send the SIGINT signal is with Control-C in most terminal programs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s