bat文件 判断服务是否存在的方法

来源:互联网 发布:姜超点评9月经济数据 编辑:程序博客网 时间:2024/05/22 06:41

转自:http://blog.sina.com.cn/s/blog_454fbf740100he1a.html

@echo off
REM 将引号内部分改成你要查找的服务名称
sc query |find /i "server" >nul 2>nul
REM 如果服务存在,跳转至exist标签
if not errorlevel 1 (goto exist) else goto notexist

:exist
REM 这里写服务存在时用的代码
goto :eof

:notexist
REM 这里写服务不存在时用的代码
goto :eof

 

====================================================================

例子:    服务名:mysql

@echo off

 

REM 将引号内部分改成你要查找的服务名称

sc query |find /i "mysql" >nul 2>nul

 

REM 如果服务存在,跳转至exist标签
if not errorlevel 1 (goto exist) else goto notexist

 

:exist

REM 服务存在时启用mysql服务
net start mysql
goto :eof

 

:notexist

REM 服务不存在时安装mysql服务
cd bin
mysqld.exe --install mysql
net start mysql
goto :eof