nodejs, 遍历所有进程, 且关闭匹配的进程

const childProcess = require('child_process');
const exec = childProcess.exec

function viewProcessMessage (name, cb) {
  let cmd = process.platform === 'win32' ? 'tasklist' : 'ps aux'
  exec(cmd, function (err, stdout, stderr) {
    if (err) {
      return console.error(err)
    }
    stdout.split('\n').filter((line) => {
      let processMessage = line.trim().split(/\s+/)
      let processName = processMessage[0] //processMessage[0]进程名称 , processMessage[1]进程id
      if (processName === name) {
        return cb(processMessage[1])
      }
    })
  })
}

viewProcessMessage('name.exe',function (msg) {
  //关闭匹配的进程
  process.kill(msg)
})

 

posted @ 2017-12-28 11:22  一夜梦想  阅读(5121)  评论(0编辑  收藏  举报