1. 程式人生 > 實用技巧 >exe註冊為service服務

exe註冊為service服務

本來這篇blog是“系統垃圾清理批處理”方面的,後面的批處理是我在工作中經常用的,經過了實踐檢驗,不會引起系統問題,平時幫公司同事、朋友等清理系統垃圾就用下面的後面批處理。 這篇blog太簡單了,所以想加入標題的內容,以充實內容。 將exe檔案註冊為service服務項,網上的方法大致為三種。如果是批處理或指令碼建議用組策略的啟動 放入處理。 第一種方式:用批處理和兩個工具 就是用Windows 2000 Resource Kit Tools中兩個小程式Instsrv.exe和Srvany.exe來生成service項。所有檔案在附件 我的測試如下: test.exe執行的效果和如下批處理相同,必須所有檔案都在C:\bin 下,我為了簡單理解所以固定目錄。 @echo off
if exist c:\heliy.txt goto appendinfo echo "no exist file heliy.txt in C partition" >> c:\heliy.txt
exit :appendinfo
@echo ""
@echo ""
@echo "===============================================================" >> c:\heliy.txt
echo "Had exist file name heliy, OK,!!!" >> c:\heliy.txt
findstr /C:"Had exist" c:\heliy.txt | find /C "Had" >> c:\heliy.txt
exit

註冊成服務的bat批處理如下: REM heliy learn from Internat @echo off REM 設定服務名稱
set service_name=testexe2 REM 設定服務描述
set service_description="heliy test this way that regesity exe of file to service item" REM 設定服務程式路徑,路徑為\\ ,不是\
set exe_path=c:\\bin\\test.exe REM 設定服務的啟動方式 auto:自動 demand:手動 disabled:禁用
set strt=auto REM ========================以下部分勿隨意修改================== REM 生成的臨時註冊檔名
set reg_file=temp_server.reg net stop %service_name% 2>nul REM 這兩行不要改動
c:\bin\instsrv.exe %service_name% remove 2>nul
c:\bin\instsrv.exe %service_name% c:\bin\srvany.exe 2>nul REM 設定服務的啟動方式 auto:自動 demand:手動 disabled:禁用
sc config %service_name% start= %strt%
sc description %service_name% "%service_description%" echo 生成登錄檔檔案...
echo Windows Registry Editor Version 5.00 >> c:\bin\%reg_file%
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%service_name%\Parameters] >> c:\bin\%reg_file%
echo "Application"="%exe_path%" >> c:\bin\%reg_file% echo 匯入登錄檔檔案...
c:\bin\%reg_file% net start %service_name% @echo 刪除臨時匯入的登錄檔檔案
del /F c:\bin\%reg_file% @echo ===========================完成============================
@pause 以上的批處理是我測試通過的,instsrv.exe 的help如下: Installs and removes system services from NT INSTSRV <service name> (<exe location> | REMOVE)
[-a <Account Name>] [-p <Account Password>] Install service example: INSTSRV MyService C:\MyDir\DiskService.Exe
-OR-
INSTSRV MyService C:\mailsrv\mailsrv.exe -a MYDOMAIN\joebob -p foo Remove service example: INSTSRV MyService REMOVE 註冊後如圖:

第二種方法:第三方工具 應用此工具,需要對exe檔案非常熟悉,工具在附件2 第三種方法:登錄檔 使用登錄檔的方法是對exe檔案很熟且exe檔案簡單,比如依賴服務、呼叫dll檔案、啟動引數等都比較簡單的情況下,一般是先根據登入身份 匯出一個service項,在根據exe的需要,修改、刪除、新增一些鍵值。 登錄檔的路徑: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services] 注意保留Enum項 啟動的型別: “Start”,DWORD值,值為2表示自動執行,值為3表示手動執行,值為4表示禁止 以下是本篇blog的原內容,是清理垃圾的批處理: ================================================================= @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\*.*"
del /f /s /q %systemroot%\SoftwareDistribution\download\*.*
del /f /s /q %systemroot%\Installer\*.msp
del /f /s /q %systemroot%\Installer\*.msi
del /f /s /q %systemroot%\Installer\*.tmp @echo 清除系統垃圾完成! --新年快樂--
@echo. & pause