Nodejs杀死本地应用(win)
2024-10-20 13:35:13
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
最新文章
- ubuntu 非常简单的方式安装多个perl版本
- Wait Type:IO_COMPLETION
- 存储过程:当基站ID大于1000的时候,把ID通过存储过程插入表,然后处理
- linux 的终端字体色和背景色的修改方法(三)
- awt可视化界面上传数据到mysql,jsp通过jdbc方式查询数据库,并将结果打印在网页上
- android 中设置HttpURLConnection 超时并判断是否超时
- VB.Net 文件处理类
- hdu 1241
- 《第一行代码》学习笔记19-广播接收器Broadcast_Receiver(2)
- Excel导入到DataTable
- hive编程指南--employees表数据定义
- jquery中 $ 和 jQuery 及 $() 的差别
- js实现菜单折叠导航
- PHP基础与JS操作的区别
- 解锁技能:sass + node-sass多页面应用编译(转载)
- 安装zabbix 遇到的一些问题
- WordCount扩展与优化
- TCP/IP 笔记 - Internet地址结构
- 【转载】 强化学习(三)用动态规划(DP)求解
- 【Unity】2.6 游戏视图(Game)
热门文章
- 基于Spark的均值漂移算法在网络舆情聚类中的应用
- Python Excel 追加数据
- 《深度探索C++对象模型》第三章 Data语意学
- Svelte框架实现表格协同文档
- VSCode 创建flutter项目和运行、调试、热更新项目
- VUE基础之:visible.sync-模态框显示隐藏、elementUI dialog组件报错或者visible属性不生效问题
- 【学习笔记】C/C++ 设计模式 - 工厂模式(上)
- magic-api数据库存储方案
- el-table 在第一行添加合计行和操作按钮
- SRS视频流服务器初试