批處理啓動和停止服務命令集批量執行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

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