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)的更多相关文章

  1. web前端效率提升-nginx+nodejs搭建本地生态

    1.起因 编写的项目是一个偏向于后台管理的web系统,使用了angular框架,在绑定数据的时候就依赖于后台的接口格式. 以前是后台写好接口后,我在绑定,在这之前一些逻辑是没法做的,有时候后台接口给的 ...

  2. nodejs:本地文件夹http服务器http-server

    一.已经安装nodejs的电脑,有一个方便通过http访问本地文件夹.文件夹服务器 static files over HTTP,并不是我们平常说的node那个web服务器哦 二.好处 可以方便实现跨 ...

  3. nodejs实现本地上传图片并预览功能(express4.0+)

    Express为:4.13.1  multyparty: 4.1.2 代码主要实现本地图片上传到nodejs服务器的文件下,通过取图片路径进行图片预览 写在前面:计划实现图片上传预览功能,但是本地图片 ...

  4. (转)nodejs搭建本地http服务器

    本文转载自:http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html 由于不做php相关的东西,懒得装apache,干脆利用no ...

  5. nodejs 构建本地web测试服务器 以及 解决访问静态资源的问题!

    直接打开html文件,是以file:///方式打开的,这种方式很多时候会遇到跨域的问题,因此我们一般会搭建一个简易的本地服务器,来运行测试页面. 一.构建静态服务器 1.使用express模块 建立个 ...

  6. nodejs 搭建本地静态服务器

    1. http-server 参看 https://www.npmjs.com/package/http-server 使用http-server搭建本地静态服务器 全局安装http-server n ...

  7. 搭建apache本地服务器·Win

    1.下载apache地址:https://www.apachelounge.com/download/ 注意:下载压缩包如下 httpd-2.4.37-win64-VC15.zip 其中根据自己电脑的 ...

  8. 使用nodeJs在本地搭建最简单的服务

    在搭建web服务器之前,需要先安装node.js 安装后node.js,接下来就需要安装http的镜像文件 一:本机安装软件 下载最新的NodeJs,进行安装.一直点击下一步就可以了.然后就可以查看安 ...

  9. nodeJS搭建本地服务器

    准备工作: 安装Node JS: 1:安装全局express:在express4.x版本中,安装时语句变为了这样: npm install -g express-generator 2:创建项目: 选 ...

  10. nodejs读取本地txt文件并输出到浏览器

    var fs = require('fs'); var chrome=""; //同步执行 function tongbu(){ var data =fs.readFileSync ...

随机推荐

  1. [编程基础] Python模块和包使用笔记

    本文探讨Python模块和Python包,这两种机制有助于模块化编程. 模块化编程是指将大型笨拙的编程任务分解为单独的,较小的,更易于管理的子任务或模块的过程.然后可以像构建模块一样将各个模块拼凑在一 ...

  2. 浅谈RMQ问题

    RMQ:question 有一个长度为 N N N的数组,数组中的数是无序的( 1 < = n < = 5 ∗ 1 0 5 1<=n<=5*10^5 1<=n<=5 ...

  3. 闻其声而知雅意,基于Pytorch(mps/cpu/cuda)的人工智能AI本地语音识别库Whisper(Python3.10)

    前文回溯,之前一篇:含辞未吐,声若幽兰,史上最强免费人工智能AI语音合成TTS服务微软Azure(Python3.10接入),利用AI技术将文本合成语音,现在反过来,利用开源库Whisper再将语音转 ...

  4. SpringBoot源码学习3——SpringBoot启动流程

    系列文章目录和关于我 一丶前言 在 <SpringBoot源码学习1--SpringBoot自动装配源码解析+Spring如何处理配置类的>中我们学习了SpringBoot自动装配如何实现 ...

  5. 移动端安卓开发学习记录--Android Studio使用adb链接夜神模拟器常用指令

    1.下载安装模拟器,打开模拟器,本步骤不再赘述 2.打开模拟机器安装路径,在地址栏输入cmd,回车,就会打开命令行窗口 3.输入 nox_adb.exe connect 127.0.0.1:62001 ...

  6. webpack动态配置多静态资源路径,动态配置多上线路径,配置less,多种图片引用方式----"webpack": "^4.41.6",

    1.项目场景是有两个静态资源目录,一个用于开发,一个用于发布,上线多个版本,打包多个版本后,也要部署到同一个服务器的同一个端口下. 根据我自己的摸索,我搞出来了下面的配置,自感觉还蛮好用的 先看我的c ...

  7. 【LeetCode】三数之和+四数之和(双指针)

    之所以放在一起是因为,"四数之和"的解题方法基本与"三数之和"一致 由此我们可以推出n数之和的解法 本质上,我们只是使用双指针的方法降低此类问题的时间复杂度 当 ...

  8. 日志添加request-id

    package com.xf.config; import java.util.Date; import javax.servlet.http.HttpServletRequest; import j ...

  9. 非线性优化-NLopt

    通过 对 一个 数学 模型 的求解 来介绍 NLopt的使用方法 数学模型: 这个是目标函数 求满足 条件的情况下 x2的开平方最小 边界约束 非线性不等式约束如下 有两个参数 x1 和 x2 ,其中 ...

  10. typora永久插入图片

    https://c.runoob.com/front-end/59 首先将图片通过在线转码工具转换成base64的编码,并用以下格式嵌入即可,格式如下: ![image] (base64) 但是由于这 ...