bat批处理命令查看两个进程是否存在
来源:互联网 发布:郭靖 杨过 知乎 编辑:程序博客网 时间:2024/05/22 06:32
第一次接触window下的批处理,领导要求写两个bat 批处理文件 互相守护 同时保证两个端口是运行的。
但是这俩个进程是相同的名称。
第一次尝试:
a.bat
判断当前是否有当前任务的进程
如果有 判断数量,调用B.bat
如果不存在 启用两个bat 调用要启动的程序 返回再判断。
@echo offgoto start :starttasklist -v | findstr /i netsafe_log > NUL if ErrorLevel 1 ( echo 启动两个端口进程 进程不存在 call G:\BAT\start4437.bat call G:\BAT\start5775.bat goto start) else ( echo "进程存在" rem for /f %%i in ('tasklist^|findstr /i netsafe_log ) do set /a n+=1rem if %n% EQU 2 ( echo 两个进程存在 call G:\BAT\AAA\CheckRunning.bat)
B.bat
@echo offgoto start :starttasklist -v | findstr /i netsafe_log > NUL if ErrorLevel 1 ( echo 启动两个端口进程 进程不存在 call G:\BAT\start4437.bat call G:\BAT\start5775.bat goto start) else ( echo "进程存在" rem for /f %%i in ('tasklist^|findstr /i netsafe_log ) do set /a n+=1rem if %n% EQU 2 ( echo 两个进程存在 call G:\BAT\AAA\checkprocess.bat)
网上还有用telnet.exe 存在状态来判断 端口是否是通的,但是这种方法不好,因为一旦加入系统服务,bat文件会杀死所有的telnet.exe 会导致 宿主机上的telnet.exe 无法应用,代码如下
mainfin.bat
@echo offgoto panduan:panduanstart /min telnet.exe 127.0.0.1 5775 ping -n 5 127.1>nultasklist|find /i "telnet.exe" > nulif %ERRORLEVEL% EQU 0 ( GOTO ok ) else ( echo 4337 service errgoto err ):errcall G:\BAT\start5775.batgoto panduan:okecho 这里杀死了 telnet.exe taskkill /F /IM telnet.exe > nulcall G:\BAT\addfin.bat
addfin.bat
@echo offgoto panduan:panduanstart /min telnet.exe 127.0.0.1 4437 ping -n 5 127.1>nultasklist|find /i "telnet.exe" > nulif %ERRORLEVEL% EQU 0 ( GOTO ok ) else ( echo 4337 service errgoto err ):errcall G:\BAT\start4437.batgoto panduan:oktaskkill /F /IM telnet.exe > nulcall G:\BAT\mainfin.bat
但是上面4个bat 文件都是有问题的,问题在资源一直没有释放 而且所占内存越来越多。 目前没有想到解决办法。 上面4个程序都不能解决时时检测端口的功能,虽然a.bat成功检测到了程序是否在进程中,但是程序在进程中不代表程序的端口的运行状态。 需要再次改进
2017年10月7日11:33:53
改进的办法是 用netstat 查看端口是在运行状态 如果是运行则继续循环 如果不在 则进行重启
@echo offtitle CheckRunninggoto panduan:panduanping -n 5 127.1>nulnetstat -ano | findstr ".*:5775\>" rem netstat -ano | findstr 0.0.0.0 | findstr 5775 | findstr LISTENING > nulif %ERRORLEVEL% EQU 0 ( GOTO ok ) else ( echo 5775 service errgoto err ):errcall G:\BAT\start5775.batecho err goto panduan:okecho ok 5775 ~~!!!rem start/wait G:\BAT\final\CheckRunning.batping -n 5 127.1>nulnetstat -ano | findstr ".*:4437\>" rem 可以加上判断端口状态的关联rem netstat -ano | findstr 0.0.0.0 | findstr 4437 | findstr LISTENING > nul if %ERRORLEVEL% EQU 0 ( GOTO ok4437 ) else ( echo 4437 service err4437goto err4437 ):ok4437goto panduan:err4437call G:\BAT\final\CheckRunning.bat
@echo offtitle checkprocessgoto panduan:panduanping -n 5 127.1>nulnetstat -ano | findstr ".*:4437\>" rem netstat -ano | findstr 0.0.0.0 | findstr 5775 | findstr LISTENING > nulif %ERRORLEVEL% EQU 0 ( GOTO ok ) else ( echo 4437 service errgoto err ):errcall G:\BAT\start4437.batecho err goto panduan:okecho ok ~~!!!call G:\BAT\final\checkprocess.batexit
最后隐藏 用vb
CreateObject("WScript.Shell").Run "cmd /c G:/BAT/final/checkprocess.bat",0
阅读全文
0 0
- bat批处理命令查看两个进程是否存在
- 批处理脚本:判断进程是否存在
- bat批处理判断文件夹是否存在并创建文件夹
- bat批处理判断文件夹是否存在并创建文件夹
- c语言实现查看进程是否存在
- BAT循环判断进程是否存在 bat 如果存在则继续判断 不存在则重新运行
- bat批处理命令大全
- 关于bat批处理命令
- .bat批处理命令
- .bat批处理命令
- bat 批处理命令 收藏
- dos bat 批处理 命令
- bat 批处理命令详解
- bat批处理命令集
- bat批处理命令
- 批处理命令 bat语法
- bat 批处理命令
- .bat批处理命令 学习
- rubycritic
- C语言的操作符
- 实现对数组{25,24,12,76,101,96,28} 的排序
- Json学习
- Nw.js开发
- bat批处理命令查看两个进程是否存在
- Java后台框架篇--Spring整合redia自动缓存管理
- 上班要打扮美美的
- 电路板书签
- hdu 1075(字典树)
- 转:通俗理解T检验与F检验的区别
- jdk8 lambda的方法引用引起的编译器bug
- java web开发中,如何查看JSP和Servlet版本
- 我的投资模型