nodejs 查看进程表

const { exec } = require("child_process");
const isWindows = process.platform == "win32";
const cmd = isWindows ? "tasklist" : "ps aux";
exec(cmd, (err, stdout, stderr) => {
  if (err) {
    return console.log(err);
  }
  // win 5列: 映像名称 PID 会话名 会话# 内存使用
  // win: ['System', '4', 'Services', '0', '152', 'K']

  // ubuntu 11列: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  // ubuntu: ['ajanuw', '317', '0.0',    '0.0', '17384',  '1952', 'tty1',   'R', '11:09',  '0:00', 'ps',     'aux']
  // console.log(stdout);
  const list = stdout
    .split("
")
    .filter(line => !!line.trim()) // 过滤空行
    .map(line => line.trim().split(/s+/))
    .filter((p, i) => (isWindows ? i > 1 : i > 0)) // 跳过头信息
    .map(p => {
      return {
        name: isWindows ? p[0] : p[10],
        id: p[1]
      };
    });
  console.log(list);
});

win

[
  { name: 'System', id: 'Idle' },
  { name: 'System', id: '4' },
  { name: 'Secure', id: 'System' },
  { name: 'Registry', id: '104' },
  { name: 'smss.exe', id: '396' },
  { name: 'csrss.exe', id: '612' },
  { name: 'wininit.exe', id: '728' },
  { name: 'services.exe', id: '804' },
  { name: 'LsaIso.exe', id: '816' },
  { name: 'lsass.exe', id: '824' },
  ...
]

ubuntu

[
  { name: '/init', id: '1' },
  { name: '/init', id: '3' },
  { name: '-bash', id: '4' },
  { name: 'node', id: '303' },
  { name: '/bin/sh', id: '310' },
  { name: 'ps', id: '311' }
]

以下代码,只在win上测试过:

const { exec } = require("child_process");

const isWindows = process.platform == "win32";
const cmd = isWindows ? "tasklist" : "ps aux";

class SystemTask {
  get() {
    return new Promise((res, rej) => {
      exec(cmd, (err, stdout, stderr) => {
        if (err) {
          return rej(err);
        }
        // win 5列: 映像名称 PID 会话名 会话# 内存使用
        // win: ['System', '4', 'Services', '0', '152', 'K']

        // ubuntu 11列: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
        // ubuntu: ['ajanuw', '317', '0.0',    '0.0', '17384',  '1952', 'tty1',   'R', '11:09',  '0:00', 'ps',     'aux']
        // console.log(stdout);
        const list = stdout
          .split("
")
          .filter(line => !!line.trim()) // 过滤空行
          .map(line => ({
            p: line.trim().split(/s+/),
            line
          }))
          .filter((_, i) => (isWindows ? i > 1 : i > 0)) // 跳过头信息
          .map(p => {
            return new Task(p);
          });
        res({ list, stdout, err, stderr });
      });
    });
  }
}

class Task {
  constructor({ p, line }) {
    this.p = p;
    this.line = line;
    this.pname = isWindows ? p[0] : p[10];
    this.pid = p[1];
  }

  kill() {
    return new Promise((res, rej) => {
      const command = isWindows
        ? `taskkill /PID ${this.pid} /TF`
        : `kill -s 9 ${this.pid}`;
      exec(command, () => res());
    });
  }

  killLikes() {
    return new Promise((res, rej) => {
      const command = isWindows
        ? `TASKKILL /F /IM ${this.pname} /T`
        : `pkill -9 ${this.pname}`;
      exec(command, () => res());
    });
  }

  start() {
    return new Promise((res, rej) => {
      exec(`${this.pname.replace(/.exe/, "")}`, () => res());
    });
  }

  async reStart() {
    await this.kill();
    await this.start();
  }
  async reStartLinks() {
    await this.killLikes();
    await this.start();
  }
}
const systemTask = new SystemTask();
systemTask.get().then(({ list }) => {
  const p = list.find(p => p.pname.toLowerCase() === "code.exe");
  if (p) {
    p.reStartLinks();
  }
});
原文地址:https://www.cnblogs.com/ajanuw/p/12298550.html