bat 学习笔记(一、打开或关闭某进程)
检查某进程是否正在运行,是就关闭它,否就跳过
@echo off
tasklist | find /i "02fdkjyq_show.exe"
REM 如果返回错误码等于1,表示没有查到进程存在转到f,如果进程存在就转到e
if "%errorlevel%"=="1" (goto f) else (goto e)
:f
echo [ %time:~,-3% ]
REM 运行某进程 注意:路径中不要有中文或空格(有空格需要特殊处理)
start /d "E:\Desktop\FDKJY002" 02FDKJYQ_Show.exe
exit
:e
echo [ %time:~,-3% ]存在进程speedfan.exe
REM 关闭cmd.exe
taskkill /f /im cmd.exe
关闭某进程
@echo off
taskkill /f /im 02fdkjyq_show.exe
https://baijiahao.baidu.com/s?id=1662138960057117121&wfr=spider&for=pc
https://jingyan.baidu.com/article/36d6ed1f8f4c571bcf48830a.html
C#执行bat文件
1.新建一个bat文件
2.将cmd命令写入bat文件中
c#代码:
Process proc = new Process();
string targetDir = string.Format(@"D:\bat\");
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = "servicerestart.bat";
proc.StartInfo.Arguments = string.Format("10");
proc.Start();
proc.WaitForExit();
持续检测进程是否存在
:start
choice /t 5 /d y /n >nul (5秒执行一次下面的指令)
tasklist|find /i "ngrok.exe" (检查是否存在ngrok.exe进程,如果检测到,下面比较的值为0,为0表示存在。)
if %errorlevel%==0 (
echo "yes"
) else (
echo "No"
start startup.bat (新窗口启动startup.bat脚本)
)
goto start (循环)
原文链接:https://blog.csdn.net/qq_40127119/article/details/109744085