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' }
]
以下代码,只在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 查看进程表的更多相关文章
- nodejs查看本机hosts文件域名对应ip
const dns = require('dns') dns.lookup('domainName', function(err, result) { console.log(result) }) r ...
- linux下的nodejs安装
linux下安装nodejs的方式: 1.源码安装 2.nvm安装 这里推荐使用nvm安装,避免下载nodejs源码: 安装步骤: 一.安装git 一般linux系统的git版本 ...
- openSUSE13.2安装Nodejs并更新到最新版
软件源中直接安装Nodejs即可 sudo zypper in nodejs 查看nodejs版本 sincerefly@linux-utem:~> node --version v0.10.5 ...
- openSUSE13.1安装Nodejs并更新到最新版
软件源中直接安装Nodejs即可 sudo zypper in nodejs 查看nodejs版本 sincerefly@linux-utem:~> node --version v0.10.5 ...
- Centos系统运行nodejs
这里我们需要先搭建一下运行的环境,直接yum安装就可以了! [root@iZwz9f80ph5u8tlqp6pi9cZ ~]# yum -y install nodejs 这里我们的环境就搭好了!安装 ...
- nodejs 环境安装
参考网站 http://www.runoob.com/nodejs/nodejs-http-server.html https://github.com/nodesource/distribution ...
- NodeJs 在window中安装使用
Nodejs: 官网下载长期版本zip格式解压 D:\Program Files\nodejs 查看版本 D:\Git\SpringBootDemo (master) $ node -v v8.11. ...
- centos上yum安装nodeJS
更新node.js各版本yum源 Node.js v8.x安装命令 curl --silent --location https://rpm.nodesource.com/setup_8.x | ba ...
- ubuntu 安装nodejs和git
1.安装curl sudo apt-get install curl 2.安装nodejs 和 npm curl -sL https://deb.nodesource.com/setup_8.x | ...
随机推荐
- Webpack4.0各个击破(10)integration篇
一. Integration 下文摘自webpack中文网: 首先我们要消除一个常见的误解,webpack是一个模块打包工具(module bundler),它不是一个任务执行工具,任务执行器是用来自 ...
- loj10014数列分段二
10014. 「一本通 1.2 练习 1」数列分段 II 题目描述 对于给定的一个长度为 n 的正整数数列 A ,现要将其分成 m 段,并要求每段连续,且每段和的最大值最小. 例如,将数列 4,2,4 ...
- SSM、SSH框架搭建,面试点总结
文章目录 1.SSM如何搭建:三个框架的搭建: 2.SSM系统架构 3.SSM整合步骤 4.Spring,Spring MVC,MyBatis,Hibernate个人总结 5.面试资源 关于SSM.S ...
- Spark DataSource Option 参数
Spark DataSource Option 参数 1.parquet 2.orc 3.csv 4.text 5.jdbc 6.libsvm 7.image 8.json 9.xml 9.1读选项 ...
- 详解Java8特性之新的日期时间 API
详解Java8特性之新的日期时间 API http://blog.csdn.net/timheath/article/details/71326329 Java8中时间日期库的20个常用使用示例 ht ...
- 将将List json 转成List<?>实体
package TestJson; import java.util.ArrayList; import java.util.List; import java.util.Map; import ne ...
- 2019牛客暑期多校训练营(第七场)E-Find the median(思维+树状数组+离散化+二分)
>传送门< 题意:给n个操作,每次和 (1e9范围内)即往数组里面插所有 的所有数,求每次操作后的中位数思路:区间离散化然后二分答案,因为小于中位数的数字恰好有个,这显然具有单调性.那么问 ...
- KMP浅谈
关于KMP KMP其实是三个人名字的缩写,因为是他们同时发现的(大佬惹不起); KMP作为CSP考点,主要亮点是其优秀的匹配复杂度,而且消耗空间小,比起hash虽然有些局限性,但是因为其正确率 ...
- UVA 10480 Sabotage (最大流最小割)
题目链接:点击打开链接 题意:把一个图分成两部分,要把点1和点2分开.隔断每条边都有一个花费,求最小花费的情况下,应该切断那些边. 这题很明显是最小割,也就是最大流.把1当成源点,2当成汇点. 问题是 ...
- c语言实现--双向循环链表操作
1,双向链表相当于两个单向循环链表. 2,双向链表的结点定义. 1 struct DULNode 2 { 3 int data; 4 struct DULNode * prior; 5 struct ...