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

最新文章

  1. ubuntu 非常简单的方式安装多个perl版本
  2. Wait Type:IO_COMPLETION
  3. 存储过程:当基站ID大于1000的时候,把ID通过存储过程插入表,然后处理
  4. linux 的终端字体色和背景色的修改方法(三)
  5. awt可视化界面上传数据到mysql,jsp通过jdbc方式查询数据库,并将结果打印在网页上
  6. android 中设置HttpURLConnection 超时并判断是否超时
  7. VB.Net 文件处理类
  8. hdu 1241
  9. 《第一行代码》学习笔记19-广播接收器Broadcast_Receiver(2)
  10. Excel导入到DataTable
  11. hive编程指南--employees表数据定义
  12. jquery中 $ 和 jQuery 及 $() 的差别
  13. js实现菜单折叠导航
  14. PHP基础与JS操作的区别
  15. 解锁技能:sass + node-sass多页面应用编译(转载)
  16. 安装zabbix 遇到的一些问题
  17. WordCount扩展与优化
  18. TCP/IP 笔记 - Internet地址结构
  19. 【转载】 强化学习(三)用动态规划(DP)求解
  20. 【Unity】2.6 游戏视图(Game)

热门文章

  1. 基于Spark的均值漂移算法在网络舆情聚类中的应用
  2. Python Excel 追加数据
  3. 《深度探索C++对象模型》第三章 Data语意学
  4. Svelte框架实现表格协同文档
  5. VSCode 创建flutter项目和运行、调试、热更新项目
  6. VUE基础之:visible.sync-模态框显示隐藏、elementUI dialog组件报错或者visible属性不生效问题
  7. 【学习笔记】C/C++ 设计模式 - 工厂模式(上)
  8. magic-api数据库存储方案
  9. el-table 在第一行添加合计行和操作按钮
  10. SRS视频流服务器初试