As http://en.wikipedia.org/wiki/POSIX_signal defines, "A signal is a limited form ofinter- process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. It is an asynchronous notification sent to a process, or to a specific thread, within the same process in order to notify it of an event that occurred."
For example, the SIGINT signal is sent to a process when its controlling terminal detects a Ctrl + C (or equivalent) keystroke. This signal tells a process that an interrupt has been requested. If a Node process has bound a callback to this event, that function might log the request prior to terminating, do some other cleanup work, or even ignore the request:
// sigint.js
console.log("Running...");
// After 16 minutes, do nothing
setInterval(() => {}, 1e6); // Keeps Node running the process
// Subscribe to SIGINT, so some of our code runs
// when Node gets that signal
process.on("SIGINT", () => {
console.log("We received the SIGINT signal!");
process.exit(1);
});
/*
$ node sigint.js
Running...
(then press Ctrl+C)
We received the SIGINT signal!
*/
Inter-Process Communication (IPC)
// ipc.js
setInterval(() => {}, 1e6);
process.on("SIGUSR1", () => {
console.log("Got a signal!");
});
SIGUSR1 (and SIGUSR2 ) are user-defined signals, triggered by no specific actionknown to the operating system. They're meant for custom functionality.
To send a command to a process, you must determine its process ID. With a PID
in hand, you can address a process and communicate with it. If the PID assigned
to ipc.js after being run through Node is 123 , then we can send that process a SIGUSR1 signal using the kill command:
$ kill –s SIGUSR1 123
A simple way to find the PID for a given Node process in UNIX is to search the system process list for the name of the program that says the process is running. If ipc.js is currently executing, its PID is found by entering the following command line in the console/terminal:
ps aux | grep ipc.js