bat批处理命令查看两个进程是否存在

第一次接触window下的批处理,领导要求写两个bat 批处理文件 互相守护 同时保证两个端口是运行的。
但是这俩个进程是相同的名称。
第一次尝试:
a.bat
判断当前是否有当前任务的进程
如果有 判断数量,调用B.bat
如果不存在 启用两个bat 调用要启动的程序 返回再判断。

@echo off

goto start 

:start
tasklist -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+=1
rem if %n% EQU 2 (
    echo   两个进程存在
    call G:\BAT\AAA\CheckRunning.bat
)

B.bat

@echo off

goto start 

:start
tasklist -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+=1
rem if %n% EQU 2 (
    echo   两个进程存在
    call G:\BAT\AAA\checkprocess.bat
)

网上还有用telnet.exe 存在状态来判断 端口是否是通的,但是这种方法不好,因为一旦加入系统服务,bat文件会杀死所有的telnet.exe 会导致 宿主机上的telnet.exe 无法应用,代码如下
mainfin.bat

@echo off

goto panduan
:panduan
start /min  telnet.exe 127.0.0.1 5775 
ping -n 5 127.1>nul
tasklist|find /i "telnet.exe" > nul
if %ERRORLEVEL% EQU 0 ( 
GOTO ok ) else ( 
echo 4337 service err
goto err  )
:err
call G:\BAT\start5775.bat
goto panduan
:ok
echo 这里杀死了 telnet.exe 
taskkill /F /IM telnet.exe > nul
call G:\BAT\addfin.bat

addfin.bat

@echo off
goto panduan
:panduan
start /min  telnet.exe 127.0.0.1 4437 
ping -n 5 127.1>nul
tasklist|find /i "telnet.exe" > nul
if %ERRORLEVEL% EQU 0 ( 
GOTO ok ) else ( 
echo 4337 service err
goto err  )
:err
call G:\BAT\start4437.bat
goto panduan
:ok
taskkill /F /IM telnet.exe > nul
call G:\BAT\mainfin.bat

但是上面4个bat 文件都是有问题的,问题在资源一直没有释放 而且所占内存越来越多。 目前没有想到解决办法。 上面4个程序都不能解决时时检测端口的功能,虽然a.bat成功检测到了程序是否在进程中,但是程序在进程中不代表程序的端口的运行状态。 需要再次改进
2017年10月7日11:33:53
改进的办法是 用netstat 查看端口是在运行状态 如果是运行则继续循环 如果不在 则进行重启

@echo off
title CheckRunning

goto panduan

:panduan
ping -n 5 127.1>nul
netstat -ano | findstr ".*:5775\>" 
rem netstat -ano | findstr  0.0.0.0 | findstr 5775 | findstr LISTENING > nul
if %ERRORLEVEL% EQU 0 ( 
GOTO ok ) else ( 
echo 5775 service err
goto err  )

:err
call G:\BAT\start5775.bat
echo err 
goto panduan

:ok
echo ok 5775 ~~!!!
rem start/wait G:\BAT\final\CheckRunning.bat

ping -n 5 127.1>nul
netstat -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 err4437
goto err4437  )

:ok4437
goto panduan

:err4437
call G:\BAT\final\CheckRunning.bat

@echo off
title checkprocess

goto panduan

:panduan

ping -n 5 127.1>nul
netstat -ano | findstr ".*:4437\>" 
rem netstat -ano | findstr  0.0.0.0 | findstr 5775 | findstr LISTENING > nul
if %ERRORLEVEL% EQU 0 ( 

GOTO ok ) else ( 

echo 4437 service err
goto err  )



:err
call G:\BAT\start4437.bat
echo err 
goto panduan


:ok
echo ok ~~!!!
call G:\BAT\final\checkprocess.bat
exit

最后隐藏 用vb

CreateObject("WScript.Shell").Run "cmd /c G:/BAT/final/checkprocess.bat",0 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值