Nodejs杀死本地应用(win)
windows端nodejs检查应用运行并杀死。
import {exec, execSync} from "child_process"
import {decode} from "iconv-lite"
import { buffer } from "stream/consumers";
//win系统 nodejs杀死本地指定应用进程 Firefox.exe
const encoding = 'cp936';
const binaryEncoding = 'binary';
function checkApplicationRun(exeName){
let cmd = process.platform === 'win32' ? `tasklist /fi "imagename eq ${exeName}" ` : `ps aux | grep ${exeName}`
// console.log(cmd)
exec(cmd, { encoding: binaryEncoding }, function (err, stdout, stderr) {
if (err) {
let resbonse = {'execstatus': false, 'msg':err}
return resbonse
}
let resbonse;
// stdout = Buffer.from(encode(stdout,'gb2312')).toString('base64');
// return stdout
stdout = decode(Buffer.from(stdout, binaryEncoding), encoding)
console.log(stdout)
stdout.split('/n').filter(function (line) {
console.log(line)
console.log(line.indexOf('No tasks'))
if (line.indexOf('No tasks')!== -1 || line.indexOf('没有运行的任务')!== -1){
console.log(exeName,'程序没有在运行');
resbonse = {'execstatus': true, 'msg':exeName+'程序没有在运行','run': false}
}
else{
// const p = line.trim().split(/\s+/) //去除前后空格并用空格分隔
// if (p[0] && p[1]) console.log('pname:' + p[0] + p[1],'PID:' + p[3] + p[4]) // 这一步可以做�?己逻辑得判�?
console.log(exeName,'程序在运行');
resbonse = {'execstatus': true, 'msg':exeName+'程序在运行','run': true}
}
return resbonse
})
})
}
function checkApplicationRunSync(exeName){
let cmd = process.platform === 'win32' ? `tasklist /fi "imagename eq ${exeName}" ` : `ps aux | grep ${exeName}`
// console.log(cmd)
let result = decode(Buffer.from(execSync(cmd)), 'gbk')
let response;
if (result.indexOf('No tasks')!== -1 || result.indexOf('没有运行的任务')!== -1){
console.log(exeName,'程序没有在运行');
response = {'execstatus': true, 'msg':exeName+'程序没有在运行','run': false}
}
else{
// const p = line.trim().split(/\s+/) //去除前后空格并用空格分隔
// if (p[0] && p[1]) console.log('pname:' + p[0] + p[1],'PID:' + p[3] + p[4]) // 这一步可以做�?己逻辑得判�?
console.log(exeName,'程序在运行');
response = {'execstatus': true, 'msg':exeName+'程序在运行','run': true}
}
return response
}
// checkApplicationRunSync('Twinkstar.exe')
function killApplication(exeName){ //未处理输出编码
const cmd = `taskkill -f /im ${exeName}`;
exec(cmd, function (err, stdout, stderr) {
if (err) {
console.log(err);
let resbonse = {'execstatus': false, 'msg':err, 'stop':false};
return resbonse
}
console.log(stdout)
let resbonse = {'execstatus': true, 'msg':exeName+'已停止运行。', 'stop':true};
return resbonse
})
}
function killApplicationSync(exeName){
const cmd = `taskkill -f /im ${exeName}`;
let result = decode(Buffer.from(execSync(cmd)), 'gbk');
// console.log(result);
return result
}
export {checkApplicationRun, checkApplicationRunSync, killApplication, killApplicationSync}
杀掉端口
npx kill-port 8080
Nodejs杀死本地应用(win)的更多相关文章
- web前端效率提升-nginx+nodejs搭建本地生态
1.起因 编写的项目是一个偏向于后台管理的web系统,使用了angular框架,在绑定数据的时候就依赖于后台的接口格式. 以前是后台写好接口后,我在绑定,在这之前一些逻辑是没法做的,有时候后台接口给的 ...
- nodejs:本地文件夹http服务器http-server
一.已经安装nodejs的电脑,有一个方便通过http访问本地文件夹.文件夹服务器 static files over HTTP,并不是我们平常说的node那个web服务器哦 二.好处 可以方便实现跨 ...
- nodejs实现本地上传图片并预览功能(express4.0+)
Express为:4.13.1 multyparty: 4.1.2 代码主要实现本地图片上传到nodejs服务器的文件下,通过取图片路径进行图片预览 写在前面:计划实现图片上传预览功能,但是本地图片 ...
- (转)nodejs搭建本地http服务器
本文转载自:http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html 由于不做php相关的东西,懒得装apache,干脆利用no ...
- nodejs 构建本地web测试服务器 以及 解决访问静态资源的问题!
直接打开html文件,是以file:///方式打开的,这种方式很多时候会遇到跨域的问题,因此我们一般会搭建一个简易的本地服务器,来运行测试页面. 一.构建静态服务器 1.使用express模块 建立个 ...
- nodejs 搭建本地静态服务器
1. http-server 参看 https://www.npmjs.com/package/http-server 使用http-server搭建本地静态服务器 全局安装http-server n ...
- 搭建apache本地服务器·Win
1.下载apache地址:https://www.apachelounge.com/download/ 注意:下载压缩包如下 httpd-2.4.37-win64-VC15.zip 其中根据自己电脑的 ...
- 使用nodeJs在本地搭建最简单的服务
在搭建web服务器之前,需要先安装node.js 安装后node.js,接下来就需要安装http的镜像文件 一:本机安装软件 下载最新的NodeJs,进行安装.一直点击下一步就可以了.然后就可以查看安 ...
- nodeJS搭建本地服务器
准备工作: 安装Node JS: 1:安装全局express:在express4.x版本中,安装时语句变为了这样: npm install -g express-generator 2:创建项目: 选 ...
- nodejs读取本地txt文件并输出到浏览器
var fs = require('fs'); var chrome=""; //同步执行 function tongbu(){ var data =fs.readFileSync ...
随机推荐
- 一步步教你在Edge浏览器上安装网风笔记
微软于2022年6月15日正式结束对浏览器"Internet Explorer (IE)"的支持,IE已正式退出历史舞台,取而代之的是目前风头正盛的被微软称为当今最好用的Edge浏 ...
- 聊聊Cookie、Session、Token 背后的故事
摘要:Cookie.Session.Token 这三者是不同发展阶段的产物 本文分享自华为云社区<Cookie.Session.Token 背后的故事>,作者: 龙哥手记. 1. 网站交互 ...
- Asp-Net-Core权限认证
title: Asp.Net Core权限认证 date: 2022-10-27 16:17:52 tags: - .NET 翻了很多的博客,文档,发现asp.net core自带的权限认证还是比较复 ...
- 发布并部署NCF站点的那些事
简介 开工第一天,祝大家2023年钱兔无量,技术兔飞猛进 为更加方便大家一站式打通所有使用NCF的环节,在新年开工的第一天给大家带来如何发布最新版本的站点 无论你的网站在开发环境做的多么的炫酷,实用, ...
- idea正则替换
将非 (股权)的替换成 股权
- UICC Send USSD 加密信息解析
已以下指令为例 已知发送 USSD 的格式为:Send USSD aaaxxxxxxxxxxxx*yyyyyyy# ;Fetch:==:SEND USSD SEND 801200001F ASSERT ...
- elk引入redis
1.引入日志依赖 <dependency> <groupId>com.cwbase</groupId> <artifactId>logback-redi ...
- 【一句话】Redis的3中缓存策略
首先一句话: 旁路缓存模式策略:写->写DB,删缓存,读->读cache,没有则读DB,然后更新到缓存 读写穿透策略:写->写缓存,然后由缓存系统写DB,读->读cache,没 ...
- 初探富文本之CRDT协同算法
初探富文本之CRDT协同算法 CRDT的英文全称是Conflict-free Replicated Data Type,最初是由协同文本编辑和移动计算而发展的,现在还被用作在线聊天系统.音频分发平台等 ...
- VS 撰写生成了多个撰写错误,其根本原因有X点,如下所列。有关详细信息,请查看CompositionException.Error属性
打开VS开发程序,莫名其妙的出现如下图错误: 网上找了很多资料,有前辈说以下方法: 解决方案如下 打开文件夹 Users\<CurrentUser>\AppData\Local\Micro ...