批处理启动和停止服务命令集批量执行dos命令

当电脑安装了数据库服务(如,Oracle、SQLServer)或一些web服务(如IIS,tomcat)时,系统会添加很多服务(services)项。而这些服务往往是占用很大的系统资源的。如果在平时不用的时候一直处于启动状态是对系统资源的极大浪费。所以我们就通过“管理工具”中的“服务”将所有不必要的服务停止。但是每次用到的时候或不用的时候手工去启动和停止比较麻烦。用批理来操作会方便很多。
下面介绍两种使用批处理操作来 设置、启动和停止服务的方法。
从Windows XP开始,其自带的DOS工具中加入了一个sc.exe(Service Control)和net.exe (C:/WINDOWS/system32)的文件,恰好可以对服务的启动类型以及服务的启动或者关闭等进行操作,再利用DOS的批处理优势,就可以把上面提到的操作进行简化。
方法 一:
sc.exe常用功能:
1、更改服务的启动状态. sc config 服务名 start= 属性(demand 手动、auto 自动、disable 禁用) 设置该服务的启动形式(= 后面有个空格)
2、删除服务(否则不建议删除任何系统服务,特别是基础服务 慎用)
3、停止或启动服务(功能上类似于net stop/start,但速度更快且能停止的服务更多)

具体的命令格式:
修改服务启动类型的命令行格式为(特别注意start=后面有一个空格)
sc config 服务名称 start= demand(设置服务为手动启动)
sc config 服务名称 start= auto(设置服务为自动启动)
sc config 服务名称 start= disabled(设置服务为禁用)
停止/启动服务的命令行格式为
sc start 服务名称
sc stop 服务名称
注意:服务名称:可以在控制面板->管理工具->服务里面,双击对应的服务显示名来查询。
例:
设置远程注册表服务为手动其格式为
sc config RemoteRegistry start= demand
设为禁用的格式为
sc config RemoteRegistry start= disabled
停止服务则格式为
sc stop RemoteRegistry
方法二:
使用net.exe功能启动或停止服务
启动/停止 服务的命令行格式为
net start 服务名称
net stop 服务名称
例:
以启动和停止Oracle服务为例:由于Oracle服务非常占用系统资源,而且平时不怎么用Oracle数据库。所以我将其设为手动启动,然后每次用到Oracle时,使用批处理来启动一堆服务。
-- 开始Oracle的服务
@echo off
net start OracleMTSRecoveryService
net start OracleOraHome92Agent
net start OracleOraHome92TNSListener
net start OracleService数据库名
-- 停止Oracle的服务
@echo off
net stop OracleService数据库名
net stop OracleOraHome92TNSListener
net stop OracleOraHome92Agent
net stop OracleMTSRecoveryService
启动方法:
方法1.保存之后便可以直接通过双击文件即可启动或者关闭服务
方法2.设置环境变量
在环境变量中的系统变量中,在PATH后面添加批处理文件的路径,不要忘记用分号;和前面的环境变量隔开,然后直接在开始菜单中的运行中输入可执行相应的文件
环境变量的作用就是告诉系统在运行中输入信息的位置而已。
注意:在vista,win7等系统有时直接运行bat文件,很多服务启动不了,会提示拒绝访问等,是因为权限问题,在运行bat文件时要以管理员身份运行即可。

net start 服务名 开启某服务
net stop 服务名 关闭某服务
注:如果服务名有空格,则用双引号括起来。另外服务名不是服务显示的名称,你要右键服务属性才能看到服务名。有时候显示的名称就是服务名。
常用一些服务的启动停止
---sqlserver 相关服务启动---
@echo.服务启动......
@echo off
@sc start MSSQLSERVER
@sc start SQLSERVERAGENT
@sc start MSSQLServerOLAPService
@sc start msftesql
@sc start MsDtsServer
@sc start SQLWriter
@echo off
@echo.启动完毕!
@pause
---sqlserver 相关服务停止---
@echo.服务停止......
@echo off
@sc stop SQLSERVERAGENT
@sc stop MSSQLServerOLAPService
@sc stop msftesql
@sc stop MsDtsServer
@sc stop SQLWriter
@sc stop MSSQLSERVER
@echo off
@echo.停止完毕!
@pause
注:每行命令前有个@,没有也可以
选择性启动一些服务
@echo off
rem echo (备注: 自动:auto 手动启动:demand 禁用:disabled )
sc config knbcenter start=disabled
sc config PPTVService start=disabled
sc config 360rp start=disabled
sc config BFAssistantSvc start=disabled
(net stop knbcenter)&(net stop PPTVService)&(net stop 360rp)&(net stop BFAssistantSvc)
echo 已禁用服务:猎豹浏览器安全中心、PPTVService、360rp、BFAssistantSvc..
echo 选择要开启的服务:
echo.
echo 0 全部
echo 1 猎豹浏览器安全中心
echo 2 PPTVService
echo 3 360杀毒实时防护
echo 4 BFAssistantSvc
echo.
set/p var=请选择要开启的服务序号:
if %var%==0 (net start knbcenter)&(net start PPTVService)&(net start 360rp)&(net start BFAssistantSvc)
if %var%==1 net start knbcenter
if %var%==2 net start PPTVService
if %var%==3 net start 360rp
if %var%==4 net start BFAssistantSvc
pause
IIS相关
IIS重新启动的dos命令:iisreset /restart
启动IIS:
net start iisadmin (IIS的整个服务)
net start w3svc (WWW网页WEB服务)
停止IIS:
net stop iisadmin /y (会自动停止www、ftp和smtp服务)
如果用的IIS自带FTP还可以执行
net start MSFtpsvc
命令来启动FTP

以上命令放到bat文件中,再结合windows的任务计划就可以实现调度
自动打开和关闭IE浏览器
使用批处理命令实现自动打开和关闭IE浏览器,把如下两行#号里的内容全部复制,粘贴到记事本,保存为*.bat文件(*号为文件名)。运行即可。
#####################
ping 127.1 -n 3 >nul
rem 延时3秒
@echo off
rem 关闭回显命令
cd C:\Program Files\Internet Explorer\
rem 改变当前目录到IE所在目录
rem 打开ie,30秒后关闭
start iexplore.exe http://localhost:8090/
rem 启动IE 执行加载路由连接程序 以上地址为例
ping 127.1 -n 30 >nul
rem 延时30秒
taskkill /im iexplore.exe /f
rem 关闭IE
rem 加载路由成功结束

#####################
-----另外附一个清理垃圾的bat文件 清理电脑中的垃圾文件.bat---
@echo off
echo 正在清除系统垃圾文件,请稍等......
del /f /s /q %systemdrive%\*.tmp
del /f /s /q %systemdrive%\*._mp
del /f /s /q %systemdrive%\*.log
del /f /s /q %systemdrive%\*.gid
del /f /s /q %systemdrive%\*.chk
del /f /s /q %systemdrive%\*.old
del /f /s /q %systemdrive%\recycled\*.*
del /f /s /q %windir%\*.bak
del /f /s /q %windir%\prefetch\*.*
rd /s /q %windir%\temp & md %windir%\temp
del /f /q %userprofile%\cookies\*.*
del /f /q %userprofile%\recent\*.*
del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
echo 清除系统垃圾文件完成!
echo. & pause
@echo off 是关闭命令回显
批量运行exe,bat文件
.\packet\dotnetfx35.exe /passive
.\packet\WindowsXP-KB968930-x86-ENG.exe /passive /norestart
.\packet\WindowsXP-KB942288-v3-x86.exe /passive /forcerestart

把上面的写在一个文本文件中,存成.bat文件,在同级目录下建一个packet的文件夹,里面放入上面三个路径中的exe文件,运行该bat文件则可以自动运行这三个文件,/passive 是运行选项,有哪些选项可以用 可以在命令行中输入 ****.exe 文件 /?则可以看到所有可用选项
/norestart 是不重启的意思  
/forcerestart 是强制重启
如果想批处理运行一系列的exe文件,均可以这么做
适合用在批量运行一系列exe文件时,如批量安装,安装补丁包
上面有一个问题就是会按顺序一个一个执行,第一个运行完才开始第二个,有时需要按顺序执行一系列的exe,bat文件,或者说同时打开则可以这样
:echo 'automatchworker'
start AutoMatchWorker.bat
:echo 'ScheduleWorker'
start ScheduleWorker.bat
:echo 'SmsWork'
start SmsWorker.bat
:echo 'NotificationWorker'
start NotificationWorker.bat
就会“同时”打开四个程序

完整的一个例子如下
echo 'kill exe'
taskkill /f /im NotificationWorker.exe
echo 'copy new exe'
XCOPY /y D:\ManagerTools\NotificationWorker D:\Workering\PushNotificationWorker
echo 'start exe'
::echo 'PushNotificationWorker'
start PushNotificationWorker.exe
先杀掉已运行的exe
copy 新的程序 /y 表示不用提示复盖
再开启
--------选择性启动某些服务-------
把下面两行#号中间的复制到bat文件中,双击运行即可
#########

@echo off

title 各种服务开启、关闭程序

:allstart

cls

echo 请选择下面的编号来启动相应的服务

echo 1.VMware 服务开启、关闭

echo 2.SQL Server 2008 服务开启、关闭

echo 3.MySQL 服务开启、关闭

echo 4.Oracle XE 服务开启、关闭

echo 5.Teamviewer 服务开启、关闭

echo 6.Alipay 支付宝 服务开启、关闭

echo e.退出

set in=

set /p in=请输入:

if "%in%"=="1" goto vmware

if "%in%"=="2" goto sqlserver

if "%in%"=="3" goto mysql

if "%in%"=="4" goto oraclexe

if "%in%"=="5" goto teamviewer

if "%in%"=="6" goto alipay

if "%in%"=="e" goto allclose

rem VMware服务开启、关闭

:vmware

echo 1.开启vm服务,2.关闭vm服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto vmstart

if "%in%"=="2" goto vmstop

if "%in%"=="3" goto vmsd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose

:vmstart

echo "正在启动VMware相关开机服务..."

rem net start ufad-ws60

net start VMAuthdService

net start VMnetDHCP

net start "VMware NAT Service"

net start VMUSBArbServicepause

echo 服务启动完毕!

goto vmware

:vmstop

echo "正在关闭VMware相关开机服务..."

rem net stop ufad-ws60

net stop VMAuthdService

net stop VMnetDHCP

net stop "VMware NAT Service"

net stop VMUSBArbService

echo 服务已关闭!

goto vmware

:vmsd

echo "切换VMware开机启动服务为手动中,请稍候..."

sc config VMAuthdService start= demand

sc config VMnetDHCP start= demand

sc config "VMware NAT Service" start= demand

sc config VMUSBArbService start= demand

echo 成功切换为手动模式!

goto vmware

rem SQL Server 2008服务开启、关闭

:sqlserver

echo 1.开启SQLServer服务,2.关SQLServer服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto sqlserverstart

if "%in%"=="2" goto sqlserverstop

if "%in%"=="3" goto sqlserversd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose

:sqlserverstart

echo "正在开启SQL Server相关开机服务"

rem SQL Server 代理 (MSSQLSERVER2008)

net start SQLAgent$MSSQLSERVER2008

rem SQL Full-text Filter Daemon Launcher (MSSQLSERVER2008)

net start MSSQLFDLauncher$MSSQLSERVER2008

rem SQL Server (MSSQLSERVER2008)

net start MSSQL$MSSQLSERVER2008

rem SQL Server Browser

net start SQLBrowser

rem SQL Server Integration Services 10.0

net start MsDtsServer100

rem SQL Server VSS Writer

net start SQLWriter

goto sqlserver

:sqlserverstop

echo "正在关闭SQL Server相关开机服务"

net stop SQLAgent$MSSQLSERVER2008

net stop MSSQLFDLauncher$MSSQLSERVER2008

net stop MSSQL$MSSQLSERVER2008

net stop SQLBrowser

net stop MsDtsServer100

net stop SQLWriter

goto sqlserver

:sqlserversd

echo "切换SQL Server开机启动服务为手动中"

sc config SQLAgent$MSSQLSERVER2008 start= demand

sc config MSSQLFDLauncher$MSSQLSERVER2008 start= demand

sc config MSSQL$MSSQLSERVER2008 start= demand

sc config SQLBrowser start= demand

sc config MsDtsServer100 start= demand

sc config SQLWriter start= demand

echo 成功切换为手动模式

goto sqlserver

rem MySQL服务开启、关闭

:mysql

echo 1.开启mysql服务,2.关闭mysql服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto mysqlstart

if "%in%"=="2" goto mysqlstop

if "%in%"=="3" goto mysqlsd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose

:mysqlstart

echo "正在启动MySQL相关开机服务..."

net start MySQL55

echo 服务启动完毕!

goto mysql

:mysqlstop

echo "正在关闭MySQL相关开机服务..."

net stop MySQL55

echo 服务已关闭!

goto mysql

:mysqlsd

echo "切换MySQL开机启动服务为手动中,请稍候..."

sc config MySQL55 start= demand

echo 成功切换为手动模式!

goto mysql

rem Oracle XE 服务开启、关闭

:oraclexe

echo 1.开启oraclexe必要服务,2.关闭oraclexe必要服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto oraclexestart

if "%in%"=="2" goto oraclexestop

if "%in%"=="3" goto oraclexesd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose1

:oraclexestart

echo "正在启动oracle xe 必要服务..."

net start OracleMTSRecoveryService

net start OracleXETNSListener

net start OracleServiceXE

echo 服务启动完毕!

goto oraclexe

:oraclexestop

echo "正在关闭oracle xe相关开机服务..."

net stop OracleMTSRecoveryService

net stop OracleXETNSListener

net stop OracleServiceXE

echo 服务已关闭!

goto oraclexe

:oraclexesd

echo "切换oracle xe开机启动服务为手动中,请稍候..."

sc config OracleMTSRecoveryService start= demand

sc config OracleServiceXE start= demand

sc config OracleServiceXE start= demand

sc config OracleXEClrAgent start= demand

sc config OracleXETNSListener start= demand

echo 成功切换为手动模式!

goto oraclexe

rem Teamviewer服务开启、关闭

:teamviewer

echo 1.开启Teamviewer服务,2.关闭Teamviewer服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto teamviewerstart

if "%in%"=="2" goto teamviewerstop

if "%in%"=="3" goto teamviewersd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose

:teamviewerstart

echo "正在启动Teamviewer相关开机服务..."

net start TeamViewer7

echo 服务启动完毕!

goto teamviewer

:teamviewerstop

echo "正在关闭Teamviewer相关开机服务..."

net stop TeamViewer7

echo 服务已关闭!

goto teamviewer

:teamviewersd

echo "切换TeamViewer开机启动服务为手动中,请稍候..."

sc config TeamViewer7 start= demand

echo 成功切换为手动模式!

goto teamviewer

rem alipay服务开启、关闭

:alipay

echo 1.开启alipay服务,2.关闭alipay服务。3.设置为手动。

echo u.回到上层。e.退出。

set in=

set /p in=请输入:

if "%in%"=="1" goto alipaystart

if "%in%"=="2" goto alipaystop

if "%in%"=="3" goto alipaysd

if "%in%"=="u" goto allstart

if "%in%"=="e" goto allclose

:alipaystart

net start AliveSvc

net start AlipaySecSvc

goto alipay

:alipaystop

net stop AliveSvc

net stop AlipaySecSvc

goto alipay

:alipaysd

sc config AliveSvc start= demand

sc config AlipaySecSvc start= demand

goto alipay

:allclose

echo 按任意键退出

pause

exit

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章