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("\n")
    .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' }
]
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("\n")
    .filter(line => !!line.trim()) // 过滤空行
    .map(line => line.trim().split(/\s+/))
    .filter((p, i) => (isWindows ? i > 1 : i > 0)) // 跳过头信息
    .map(p => {
      const pname = isWindows ? p[0] : p[10];
      const pid = p[1];
      return new Task(pid, pname);
    });

  const p = list.find(p => p.pname.toLowerCase() === "code.exe");
  if (p) {
    p.reStartLinks();
  }
});

class Task {
  constructor(pid = "", pname = "") {
    this.pid = pid;
    this.pname = pname;
  }

  kill() {
    return new Promise((res, rej) => {
      if (isWindows) {
        exec(`taskkill /PID ${this.pid} /TF`, () => res());
      }
    });
  }

  killLikes() {
    return new Promise((res, rej) => {
      if (isWindows) {
        exec(`TASKKILL /F /IM ${this.pname} /T`, () => res());
      }
    });
  }

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

  async reStart() {
    await this.kill();
    await this.start();
  }
  async reStartLinks() {
    await this.killLikes();
    await this.start();
  }
}

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/12298550.html
今日推荐