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
posted @ 2022-05-30 11:39  hanwang~  阅读(298)  评论(0编辑  收藏  举报