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();
      }
    });
    
  • 相关阅读:
    使用VS2012开发基于Office 2013的AddIn程序
    Silverlight Virtualization Overview
    解除.NET中虚拟路径的疑惑
    T4 Template Overview
    Silverlight ItemsControl详细解析+解惑
    C#多线程词频统计修改
    C#多线程词频统计
    通讯录从诺基亚手机同步至iphone
    验证邮箱合法性的一些测试样例
    九度1131:合唱队形
  • 原文地址:https://www.cnblogs.com/ajanuw/p/12298550.html
Copyright © 2011-2022 走看看