kill

process.kill(pid, signal=’SIGTERM’)

Send a signal to a process. pid is the process id and signal is the string describing the signal to send. The default signal is SIGTERM. Here are some sample signals from the Mac OSX man pages:

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

Note that just because the name of this function is process.kill, it is really just a signal sender, like the kill system call. The signal sent may do something other than kill the target process.

Example of sending a signal to yourself:

process.on('SIGHUP', function () {
  console.log('Got SIGHUP signal.');
});

setTimeout(function () {
  console.log('Exiting.');
  process.exit(0);
}, 100);

process.kill(process.pid, 'SIGHUP');
$ node doc_test.js 
Got SIGHUP signal.
Exiting.

This function also has the potential for failure given insufficient permissions:

setTimeout(function () {
  console.log('Exiting.');
  process.exit(0);
}, 100);

try {
  // try to terminate a root owned process
  process.kill(16);
}
catch(err) {
  console.error("Failed to send signal to process: " + err);
}
$ node doc_test.js 
Failed to send signal to process: Error: EPERM, Operation not permitted
Exiting.

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