zoukankan      html  css  js  c++  java
  • 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();
      }
    });
    
  • 相关阅读:
    2016"百度之星"
    codeforces 55 div2 C.Title 模拟
    codeforces 98 div2 C.History 水题
    codeforces 97 div2 C.Replacement 水题
    codeforces 200 div2 C. Rational Resistance 思路题
    bzoj 2226 LCMSum 欧拉函数
    hdu 1163 九余数定理
    51nod 1225 余数的和 数学
    bzoj 2818 gcd 线性欧拉函数
    Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学
  • 原文地址:https://www.cnblogs.com/ajanuw/p/12298550.html
Copyright © 2011-2022 走看看