ps
A Node.js module for looking up running processes.
NOTE: This version do not use command and command arguments. Use a single long string with the command and all its arguments joined with a white space. This way it is easier to search the PS CMD column (command and arguments) with a complex RegExp - equals ps aux | grep
.
Install
Usage
Lookup process with spicified pid
:
var ps = require ( ' ps-node ' ) ;
ps . lookup ( { pid : 12345 } , function ( err , resultList ) {
if ( err ) {
throw new Error ( err ) ;
}
var process = resultList [ 0 ] ;
if ( process ) {
console . log ( ' PID: %s, COMMAND: %s ' , process . pid , process . command ) ;
}
else {
console . log ( ' No such process found! ' ) ;
}
} ) ;
Or use RegExp to filter command
:
var ps = require ( ' ps-node ' ) ;
ps . lookup ( {
command : ' node ' ,
} , function ( err , resultList ) {
if ( err ) {
throw new Error ( err ) ;
}
resultList . forEach ( function ( process ) {
if ( process ) {
console . log ( ' PID: %s, COMMAND: %s ' , process . pid , process . command ) ;
}
} ) ;
} ) ;
Also, you can use kill
to kill process by pid
:
var ps = require ( ' ps-node ' ) ;
ps . kill ( ' 12345 ' , function ( err ) {
if ( err ) {
throw new Error ( err ) ;
}
else {
console . log ( ' Process %s has been killed! ' , pid ) ;
}
} ) ;
You can also pass arguments to lookup
with psargs
as arguments for ps
command(Note that psargs
is not available in windows):
var ps = require ( ' ps-node ' ) ;
ps . lookup ( {
command : ' node ' ,
psargs : ' ux '
} , function ( err , resultList ) {
if ( err ) {
throw new Error ( err ) ;
}
resultList . forEach ( function ( process ) {
if ( process ) {
console . log ( ' PID: %s, COMMAND: %s ' , process . pid , process . command ) ;
}
} ) ;
} ) ;
Lastly, you can filter a list of items by their PPID by passing a PPID to filter on. You will need to pass in a psarg
that provides the PPID in the results (-l
or -j
for instance).
var ps = require ( ' ps-node ' ) ;
ps . lookup ( {
command : ' mongod ' ,
psargs : ' -l ' ,
ppid : 82292
} , function ( err , resultList ) {
if ( err ) {
throw new Error ( err ) ;
}
resultList . forEach ( function ( process ) {
if ( process ) {
console . log ( ' PID: %s, COMMAND: %s ' , process . pid , process . command ) ;
}
} ) ;
} ) ;