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. SSH框架搭建详细步骤整理

    学习Java面前有两座山,一座山叫SSM,一座山叫SSH,跨越了这两座山之后才能感受到这个语言的魅力所在,SSM框架的搭建详细在之前博客已经涉及了,今天来整理SSH框架详细步骤: 生有涯 而 学无涯 ...

  2. Java中Socket的用法

    Socket分为ServerSocket和Socket两大类: 其中ServerSocket用于服务器端,可以通过accept方法监听请求,监听到请求后返回Socket: Socket用户具体完成数据 ...

  3. (6)dd命令安装Linux

    1.面对大批量服务器的安装,人们往往热衷于选择"无人值守安装"的方式,而此方式需要对服务器进行过多的配置,并不适合初学者. 无人值守安装(Kickstart),又称全自动安装,其工 ...

  4. Spring MVC 处理一个请求的流程分析

    Spring MVC是Spring系列框架中使用频率最高的部分.不管是Spring Boot还是传统的Spring项目,只要是Web项目都会使用到Spring MVC部分.因此程序员一定要熟练掌握MV ...

  5. JavaScript里处理字符串的一些常用方法

    1.length 属性返回字符串的长度 let srt = "hello world!"; console.log(srt.length) // 12 2.indexOf() 方法 ...

  6. Codeforces Round #316 (Div. 2) D. Tree Requests(dsu)

    题目链接 题意:对于m次询问 求解以vi为根节点 深度为hi的的字母能不能组合成回文串. 思路:暴力dsu找一边 简直就是神技! #include<bits/stdc++.h> #defi ...

  7. Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)

    题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...

  8. Centos7 搭建Nginx+rtmp+hls直播推流服务器

    1 准备工具 使用yum安装git [root~]# yum -y install git 下载nginx-rtmp-module,官方github地址 // 通过git clone 的方式下载到服务 ...

  9. PowerShell随笔4---变量

    全局变量 输入$global:后按ctrl+space,我们就可以看到所有的全局变量. 比如我们可以查看PowerShell的版本: 我们可以在在编写脚本代码的时候使用这些变量,globle可以省略, ...

  10. Cobalt Strike特征隐藏

    前言 首先红蓝对抗的时候,如果未修改CS特征.容易被蓝队溯源. 前段时间360公布了cobalt strike stage uri的特征,并且紧接着nmap扫描插件也发布了.虽说这个特征很早就被发现了 ...