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 | ...
随机推荐
- Redis击穿、穿透、雪崩产生原因以及解决思路
击穿 大家都知道,计算机的瓶颈之一就是IO,为了解决内存与磁盘速度不匹配的问题,产生了缓存,将一些热点数据放在内存中,随用随取,降低连接到数据库的请求链接,避免数据库挂掉.需要注意的是,无论是击穿还是 ...
- Python Package(转)
http://www.cnpythoner.com/post/2.html python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事 ...
- Codeforces Round #655 (Div. 2) C. Omkar and Baseball
题目链接:https://codeforces.com/contest/1372/problem/C 题意 给出一个大小为 $n$ 的排列,每次操作可以选取一个连续子数组任意排列其中的元素,要求每个元 ...
- Codeforces Round #646 (Div. 2) 题解 (ABCDE)
目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...
- KMP && Manacher && 扩展KMP整理
KMP算法: kmp示例代码: void cal_next(char *str, int *next, int len) { next[0] = -1;//next[0]初始化为-1,-1表示不存在相 ...
- 洛谷 P1429 平面最近点对(加强版) (分治模板题)
题意:有\(n\)个点对,找到它们之间的最短距离. 题解:我们先对所有点对以\(x\)的大小进行排序,然后分治,每次左右二等分递归下去,当\(l+1=r\)的时候,我们计算一下距离直接返回给上一层,若 ...
- 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者 B.牛牛摆放花 (贪心)
题意;将一组数重新排序,使得相邻两个数之间的最大差值最小. 题解:贪心,现将所有数sort一下,然后正向遍历,将数分配到新数组的两端,然后再遍历一次维护一个最大值即可. 代码: class Solut ...
- 4.PowerShell DSC核心概念之配置
什么是配置 DSC 配置是定义某一特殊类型函数的 PowerShell 脚本. 配置的语法 Configuration MyDscConfiguration { #配置块 Import-DscReso ...
- [Golang]-2 Map关联数组与下划线(_)的意义
目录 map 下划线(underscore) 用在import 用在返回值 用在变量 map map 是 Go 内置关联数据类型(在一些其他的语言中称为哈希 或者字典 ). func main() { ...
- Docker应用场景和局限性
Docker有哪些好的特性?作为一种新兴的虚拟化方式,Docker跟传统的虚拟化方式相比具有众多的优势.首先, Docker容器的启动可以在秒级实现,这相比传统的虚拟机方式要快得多.其次, Docke ...