node.js how to check a process is running by the process name?

后端 未结 6 1125
一个人的身影
一个人的身影 2021-01-03 20:58

i run node.js in linux, from node.js how to check if a process is running from the process name ? Worst case i use child_process, but wonder if better ways ?

Thanks

6条回答
  •  情歌与酒
    2021-01-03 21:45

    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.

提交回复
热议问题