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' }
]

以下代码,只在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("\n")
.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();
}
});

nodejs 查看进程表的更多相关文章

  1. nodejs查看本机hosts文件域名对应ip

    const dns = require('dns') dns.lookup('domainName', function(err, result) { console.log(result) }) r ...

  2. linux下的nodejs安装

      linux下安装nodejs的方式: 1.源码安装 2.nvm安装 这里推荐使用nvm安装,避免下载nodejs源码:   安装步骤: 一.安装git        一般linux系统的git版本 ...

  3. openSUSE13.2安装Nodejs并更新到最新版

    软件源中直接安装Nodejs即可 sudo zypper in nodejs 查看nodejs版本 sincerefly@linux-utem:~> node --version v0.10.5 ...

  4. openSUSE13.1安装Nodejs并更新到最新版

    软件源中直接安装Nodejs即可 sudo zypper in nodejs 查看nodejs版本 sincerefly@linux-utem:~> node --version v0.10.5 ...

  5. Centos系统运行nodejs

    这里我们需要先搭建一下运行的环境,直接yum安装就可以了! [root@iZwz9f80ph5u8tlqp6pi9cZ ~]# yum -y install nodejs 这里我们的环境就搭好了!安装 ...

  6. nodejs 环境安装

    参考网站 http://www.runoob.com/nodejs/nodejs-http-server.html https://github.com/nodesource/distribution ...

  7. NodeJs 在window中安装使用

    Nodejs: 官网下载长期版本zip格式解压 D:\Program Files\nodejs 查看版本 D:\Git\SpringBootDemo (master) $ node -v v8.11. ...

  8. centos上yum安装nodeJS

    更新node.js各版本yum源 Node.js v8.x安装命令 curl --silent --location https://rpm.nodesource.com/setup_8.x | ba ...

  9. ubuntu 安装nodejs和git

    1.安装curl sudo apt-get install curl 2.安装nodejs 和 npm curl -sL https://deb.nodesource.com/setup_8.x | ...

随机推荐

  1. 作为一款内存数据库,为什么断电后Redis数据不会丢失

    前言 Redis 作为一款内存数据库,被广泛使用于缓存,分布式锁等场景,那么假如断电或者因其他因素导致 Reids 服务宕机,在重启之后数据会丢失吗? Redis 持久化机制 Redis 虽然是定义为 ...

  2. 《我想进大厂》之Zookeeper夺命连环9问

    谈谈你对Zookeeper的理解? Zookeeper是一个开源的分布式协调服务,由雅虎公司创建,由于最初雅虎公司的内部研究小组的项目大多以动物的名字命名,所以后来就以Zookeeper(动物管理员) ...

  3. Java泛型中的通配符T,E,K,V

    Java泛型中的通配符T,E,K,V 1.泛型的好处 2.泛型中的通配符 2.1 T,E,K,V,? 2.2 ?无界通配符 2.3 上界通配符 < ? extends E> 2.4 下界通 ...

  4. snmp协议 及snmpwalk

    推荐阅读: snmp及工具:https://www.jianshu.com/p/dc2dc0222940 snmp协议详解:https://blog.csdn.net/shanzhizi/articl ...

  5. checkbox限制选中个数

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. 3. Linux常用系统状态检测命令

    1.ifconfig :于获取网卡配置与网络状态等信息,如网卡名称.IP.MAC等 2.uname -a :完整地查看当前系统的内核名称.主机名.内核发行版本.节点名.系统时间.硬件名称.硬件平台.处 ...

  7. VIT Vision Transformer | 先从PyTorch代码了解

    文章原创自:微信公众号「机器学习炼丹术」 作者:炼丹兄 联系方式:微信cyx645016617 代码来自github [前言]:看代码的时候,也许会不理解VIT中各种组件的含义,但是这个文章的目的是了 ...

  8. JVM之堆体系结构

    1.Heap堆(Java7之前) 一个JVM实例只存在一个堆内存,堆内存的大小是可以调节的.类加载器读取了类文件后,需要把类.方法.常变量放到堆内存中,保存所有引用类型的真实信息,以方便执行器执行,堆 ...

  9. validate插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. MySQL常用SQL语句1

    -- 创建表 CREATE TABLE USER ( -- id默认每个表都该拥有(非空 不重复) -- (id是每一行的唯一标识) -- 其他字段可能会重复,我们无法依赖其他的字段去确定某一行记录 ...