node.js how to check a process is running by the process name?
A little improvement of code answered by d_scalzi. Function with callback instead of promises, with only one variable query and with switch instead of if/else.
const exec = require('child_process').exec;
const isRunning = (query, cb) => {
let platform = process.platform;
let cmd = '';
switch (platform) {
case 'win32' : cmd = `tasklist`; break;
case 'darwin' : cmd = `ps -ax | grep ${query}`; break;
case 'linux' : cmd = `ps -A`; break;
default: break;
}
exec(cmd, (err, stdout, stderr) => {
cb(stdout.toLowerCase().indexOf(query.toLowerCase()) > -1);
});
}
isRunning('chrome.exe', (status) => {
console.log(status); // true|false
})
The following should work. A process list will be generated based on the operating system and that output will be parsed for the desired program. The function takes three arguments, each is just the expected process name on the corresponding operating system.
In my experience ps-node takes WAY too much memory and time to search for the process. This solution is better if you plan on frequently checking for processes.
const exec = require('child_process').exec
function isRunning(win, mac, linux){
return new Promise(function(resolve, reject){
const plat = process.platform
const cmd = plat == 'win32' ? 'tasklist' : (plat == 'darwin' ? 'ps -ax | grep ' + mac : (plat == 'linux' ? 'ps -A' : ''))
const proc = plat == 'win32' ? win : (plat == 'darwin' ? mac : (plat == 'linux' ? linux : ''))
if(cmd === '' || proc === ''){
resolve(false)
}
exec(cmd, function(err, stdout, stderr) {
resolve(stdout.toLowerCase().indexOf(proc.toLowerCase()) > -1)
})
})
}
isRunning('myprocess.exe', 'myprocess', 'myprocess').then((v) => console.log(v))
You can use the ps-node package.
https://www.npmjs.com/package/ps-node
var ps = require('ps-node');
// A simple pid lookup
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, ARGUMENTS: %s', process.pid, process.command, process.arguments );
}
});
});
I believe you'll be looking at this example. Check out the site, they have plenty of other usages. Give it a try.
Just incase you are not bound to nodejs, from linux command line you can also do ps -ef | grep "YOUR_PROCESS_NAME_e.g._nodejs"
to check for a running process.
Here's another version of the other answers, with TypeScript and promises:
export async function isProcessRunning(processName: string): Promise<boolean> {
const cmd = (() => {
switch (process.platform) {
case 'win32': return `tasklist`
case 'darwin': return `ps -ax | grep ${processName}`
case 'linux': return `ps -A`
default: return false
}
})()
return new Promise((resolve, reject) => {
require('child_process').exec(cmd, (err: Error, stdout: string, stderr: string) => {
if (err) reject(err)
resolve(stdout.toLowerCase().indexOf(processName.toLowerCase()) > -1)
})
})
}
const running: boolean = await isProcessRunning('myProcess')