最近用到 java,用tomcat起的服务,经常服务关了,对应的进程还在跑,导致再次启动服务失败,需要手动关闭进程。

使用 dos命令虽然只有两行,总是输,也很烦。

netstat -ano | findstr  8080    //查找8080端口,对应的进程

taskkill /pid  5684  /f      //关闭进程 5684

于是就想着,写一个 .bat 的文件,每次直接点击一下就好,可惜 dos命令玩不熟,折腾一番,决定放弃,换自己熟悉的语言 node来,然后使用 pkg 将node文件打包为 .exe 文件;

node版本太低不行,反正我的 7.16失败,后面安装了 8.x才行

npm install -g pkg
npm i node-cmd

  

  

index.js

var cmd=require('node-cmd');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdout.write('请输入端口:'); //标准输出
process.stdin.on('data', function (data) {
var str = data.slice(0, -2);
process.stdin.emit('end');
var num = parseInt(str);
if(num){
getProcessId(num);
}else{
process.stdout.write('输入的端口无效:'+str);
} });
process.stdin.on('end', function () { }); function getProcessId(port){
cmd.get(
'netstat -ano | findstr '+ port, //查找端口对应的进程
function(err, data, stderr){
//console.log(err, data, stderr);
if(err){
process.stdout.write(port +"端口没有被占用");
}else{
var arr = data.split("\r\n");
var cache = {};
for(var i = 0; i < arr.length; i++){
var item = arr[i];
if(item){
var pid = getPid(item);//提取出的进程id
pid = parseInt(pid);
console.log(item);
//console.log(port);
if(pid && !cache[pid]){
cache[pid] = true; //防止重复的杀进程
console.log("开始关闭进程:"+pid)
killProcess(pid);
}
}
}
}
cmd.get(
'PAUSE', //等待..
function(err, data, stderr){
});
}); //taskkill /pid 13064 /f cmd.run('touch example.created.file');
} function getPid(item){
var p = '';
for(var i = item.length -1; i > 0 ; i--){
if(item[i] == ' '){
return item.substring(i);
}
}
return 0;
} function killProcess(pid){
cmd.get(
'taskkill /pid '+ pid +' /f', //杀掉对应的进程
function(err, data, stderr){
if(err){
console.log(err);
}else{
console.log("关闭"+pid+"进程成功");
} });
}

  

打包命令:注意node版本不能太低

pkg -t win  index.js

打包完成之后,直接双击运行,输入端口,就会关掉对应的进程

关闭 进程,还可以用node子进程来实现

const c = require('child_process');

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdout.write('请输入端口:'); //标准输出
process.stdin.on('data', function (data) {
var str = data.slice(0, -2);
process.stdin.emit('end');
var num = parseInt(str);
if(num){
getProcessId(num);
}else{
process.stdout.write('输入的端口无效:'+str);
} });
process.stdin.on('end', function () { }); function getProcessId(port){
c.exec(`netstat -ano|findstr ${port}`, function(error, stdout, stderr){
if(error){
console.log("该端口没有被占用");
}else{
var arr = stdout.split("\r\n");
var cache = {};
for(var i = 0; i < arr.length; i++){
var item = arr[i];
if(item){
var pid = getPid(item);//提取出的进程id
pid = parseInt(pid);
if(pid && !cache[pid]){
cache[pid] = true; //防止重复的杀进程
console.log("开始关闭进程"+pid)
killProcess(pid);
}
}
}
}
c.exec("pause",function(){ });
});
}
function getPid(item){
var p = '';
for(var i = item.length -1; i > 0 ; i--){
if(item[i] == ' '){
return item.substring(i);
}
}
return 0;
}
function killProcess(pid){
c.exec(`taskkill /pid ${pid} /f`,function(error, stdout, stderr){
if(error){
console.log(error);
}else{
console.log("关闭"+pid+"进程成功");
}
})
}

  

node代码打包为 exe文件---端口进程关闭demo的更多相关文章

  1. 32-python代码打包成exe文件-pyinstaller

    安装 pyinstaller: pip install pyinstall 简单使用 最简单的使用方式是运行 pyinstaller myscript.py 来生成可执行文件,其中 myscript. ...

  2. python代码打包成exe文件

    1.准备工作 1.安装pywin32.pyinstaller 2.准备好ico文件 找一个png格式的图片,使用png2ico脚本生成包含以下6个尺寸的ico文件:128×128 64×64 48×4 ...

  3. win7下用PyInstaller把Python代码打包成exe文件

    2013-11-05 22:02:14|   1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...

  4. 将Java代码打包成jar文件转换为.exe可执行程序方法

    ​ 做完学生管理系统后我想将代码打包成一个可执行程序,那样就可以直接点击在Windows下运行了,下面就跟大家分享下打包方法. 将Java的代码转换成 .exe 文件需要先把代码打包成 jar 文件然 ...

  5. 控件注册 - 利用资源文件将dll、ocx打包进exe文件(C#版)

    原文:控件注册 - 利用资源文件将dll.ocx打包进exe文件(C#版) 很多时候自定义或者引用控件都需要注册才能使用,但是如何使要注册的dll或ocx打包到exe中,使用户下载以后看到的只是一个e ...

  6. Python笔记(一):安装+爬虫环境配置+打包为EXE文件

    1.     安装 https://www.python.org/downloads/windows/  到官网下载安装程序 Windows x86        32位操作系统 Windows x8 ...

  7. Pyinstaller (python打包为exe文件)

    需求分析: python脚本如果在没有安装python的机器上不能运行,所以将脚本打包成exe文件,降低脚本对环境的依赖性,同时运行更加迅速. 当然打包的脚本似乎不是在所有的win平台下都能使用,wi ...

  8. Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)

    Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer   Advanced Installer :Free for 30 da ...

  9. 将 Python3 文件打包成 exe 文件

    我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...

随机推荐

  1. 第一章:初识Python

    一个Python列表 movies = ["The Holy Grail",1975,"Terry Jones&Terry Gilliam",91,[& ...

  2. tsm 切记

    切记不可删除节点,如果删除下面带的数据也会删除

  3. 洛谷1443 马的遍历【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1443 题意: 给一个n*m的棋盘,马在上面走(规则就是象棋中的规则,详细见代码dx,dy数组定义) 问棋盘上 ...

  4. .NET Core开发日志——Global Tools

    .NET Core 2.1引入了一个新的功能,Global Tools,其本质是包含控制台应用程序的nuget包,目前而言,还没有特别有用的工具,不过相信随着时间的推移,各种有创意或者实用性强的Glo ...

  5. [No000015C]计算机科学关键领域

  6. [No0000C8]英特尔快速存储IRST要不要装

    IRST (英特尔快速存储技术)Intel Rapid Storage Technology(简称IRST)又称英特尔快速存储技术. Intel Rapid Start Technolog同样是简称( ...

  7. C内存模型

    C语言中内存分布及程序运行中(BSS段.数据段.代码段.堆栈) - 秦宝艳的个人页面 - 开源中国 https://my.oschina.net/pollybl1255/blog/140323

  8. take a cpu core offline

    [root@vrouter1 ~]# cat /sys/devices/system/cpu/online -,,- [root@vrouter1 ~]# cat /sys/devices/syste ...

  9. Linux忘记密码常用的几种解决方法

    原文链接:https://www.cnblogs.com/vurtne-lu/p/6550590.html 一. lilo引导1. 在出现 lilo: 提示时键入 linux single Boot: ...

  10. scapy 中的ARP

    scapy 常用命令 ls() //查看支持的协议对象lsc() //查看函数show() //查看数据包结构send() //三层发包sr() //三层收发sr1() //三层收发只收一 timeo ...