Find processes and watch ports for listener processes using a simple API
Using npm
$ npm install process-finder --save
Finds processes listening on the provided port
, invokes fn
with an array of pids
listening on that port.
data
is the output of the command that checks the status of the port. internal
will contain any internal errors that might have been suppressed.
var finder = require('process-finder');
var port = 3000;
finder.find(port, function(err, pids){
// do something about the pids listening on the port, i.e:
pids.forEach(process.kill);
});
Port can be an object of options
, or just a port.
{
port: 3000, // the port we listen on
elevate: false, // whether to use `sudo`, to get elevated priviledges
}
Returns an EventEmitter
that allows us to track a port's listening processes.
Port can be an object of options
, or just a port.
{
port: 3000, // the port we listen on
frequency: 200, // the frequency with which the port is scanned for changes
elevate: false, // whether to use `sudo`, to get elevated priviledges
info: false // if enabled, a verbose `info` event will be emitted
}
var finder = require('process-finder');
var port = 3000; // port to watch
var watcher = finder.watch(port);
watcher.on('listen', function(pid){
console.log(pid + ' listening on port ' + port);
});
watcher.on('unlisten', function(pid){
console.log(pid + ' no longer listening on port ' + port);
});
watcher.on('error', console.error);
watcher.on('update', function(pids){
console.log('updated! listeners:', pids);
});
Stops a watch
. It will no longer emit events.
Starts a watch which was previously stopped using .stop()
Force-restart the watch.
Watch extends the EventEmitter
prototype, and provides a few events you can listen for.
Triggers when the watch is started, either when manually started, or when a new watch is created.
Triggers when a watch is stopped.
Triggers when a pid
is detected to be listening on the port we're watching. The pid
is passed as an argument.
Triggers when a pid
is detected to be no longer listening on the port we're watching. The pid
is passed as an argument.
Triggers every time the port is scanned for changes.
data
is the output of the command that checks the status of the port. internal
will contain any internal errors that might have been suppressed.
Triggers whenever an error is thrown.
The CLI allows you to wait on a port
procfinder --wait --port 3000
Once a process starts listening on that port, the CLI will output its pid
and exit.