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 | ...
随机推荐
- 深度学习论文翻译解析(十八):MobileNetV2: Inverted Residuals and Linear Bottlenecks
论文标题:MobileNetV2: Inverted Residuals and Linear Bottlenecks 论文作者:Mark Sandler Andrew Howard Menglong ...
- 通过脚本本地下载Jar包
通过脚本本地下载Jar包 1.脚本 2.pom.xml 1.脚本 download.bat # !/bin/bash mvn -f pom.xml dependency:copy-dependenci ...
- Linux 使用Vmware克隆,修改克隆机器内容
在Vmware中搭建好一台虚拟机,拍照快照,然后克隆其他集群进行练习,克隆后的机器都需要修改的内容有如下几点: 1:各机器之间,在网络上能够互相ping通 每台机器的IP地址必须是唯一的. 进入 ca ...
- php之__DIR__,__FILE__,getcwd()的区别。
__DIR__ 在哪个脚本文件里面出现,就显示当前脚本的目录,不包含文件名.假如目录A下的1.php包含了这个魔术常量,这个文件被目录B下的2.php调用了.那么__DIR__返回的值是多少呢?返回的 ...
- CCF CSP 202012-2 期末预测之最佳阈值
202012-2 期末预测之最佳阈值 题目背景 考虑到安全指数是一个较大范围内的整数.小菜很可能搞不清楚自己是否真的安全,顿顿决定设置一个阈值 θ,以便将安全指数 y 转化为一个具体的预测结果--&q ...
- OLAP、OLTP的介绍和比较(转载)
OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...
- Codeforces Global Round 11 A. Avoiding Zero(前缀和)
题目链接:https://codeforces.com/contest/1427/problem/A 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正.负前缀和,如 ...
- Codeforces Round #533 (Div. 2) B. Zuhair and Strings(字符串)
#include <bits/stdc++.h> using namespace std; int main() { int n,k;cin>>n>>k; stri ...
- AtCoder Beginner Contest 162
比赛链接:https://atcoder.jp/contests/abc162/tasks A - Lucky 7 #include <bits/stdc++.h> using names ...
- hdu5637 Transform (bfs+预处理)
Problem Description A list of n integers are given. For an integer x you can do the following operat ...