windows in nginx.exe registered as a system service

1, nginx.exe statistical bin directory to create a folder and put it into written script registered for the service.

Installservice.bat、myconf.conf、myIstall.exe、UninstallService.bat

 

2, modify myconf.conf, configure, if you do not need to ngnix registered service adjustments, To register you need to configure other services under the service name and exe name.

3. Double-click Installservice.bat, nginx.exe can be registered as a system service.

 

Installservice.bat says:

@echo off



setlocal
:: 当前脚本所在目录
set batHome=%~dp0
cd %batHome%
cd ../
:: exe所在目录
set exeHome=%CD%



:: 从配置文件读取设置的脚本名称、服务名称、待注册服务的exe
for /f "eol=# tokens=1,2 delims==" %%i in (%batHome%myconf.conf) do (
	if /I "%%i"=="batName" set batName=%%j
	if /I "%%i"=="serviceName" set serviceName=%%j
	if /I "%%i"=="exeName" set exeName=%%j
)



:: 复制myInstall.exe到nginx.exe所在目录
copy %batHome%%batName%.exe %exeHome%\%batName%.exe /Y



rem  自动生成注册服务文件xml 
:: 自动生成与myInstall.exe对应的myInstall.xml
echo ^<service^> > ./%batName%.xml
echo	 ^<id^>%serviceName%^</id^>  >> ./%batName%.xml
echo	 ^<name^>%serviceName%^</name^> >> ./%batName%.xml 
echo	 ^<description^>%serviceName%^</description^> >> ./%batName%.xml
echo	 ^<logpath^>%exeHome%\logs\^</logpath^> >> ./%batName%.xml
echo	 ^<logmode^>roll^</logmode^> >> ./%batName%.xml
echo	 ^<executable^>%exeHome%\%exeName%^</executable^> >> ./%batName%.xml
echo	 ^<stopexecutable^>%exeHome%\%exeName% -s stop^</stopexecutable^> >> ./%batName%.xml
echo ^</service^> >> ./%batName%.xml



:: 注册exe为服务
%batName% install
if not errorlevel 1 goto :eof
endlocal
pause

Myconf.conf content is as follows:

#需要注册服务使用到脚本的名称,如下myInstall.exe和myInstall.xml中的名称myInstall
batName=myInstall
#需要注册服务的名称,如需要注册nginx服务
serviceName=nginx
#exe名称
exeName=nginx.exe

UninstallService.bat says:

@echo off


setlocal
:: 当前脚本所在目录
set batHome=%~dp0


:: 从配置文件读取设置的服务名称
for /f "eol=# tokens=1,2 delims==" %%i in (%batHome%myconf.conf) do (
	if /I "%%i"=="serviceName" set serviceName=%%j
)


::删除系统服务exe
sc delete %serviceName%

endlocal
pause

myIstall.exe registered service auxiliary exe, actually using the Windows Service Wrapper tool to register the service, here renamed to myInstall.exe

Published 98 original articles · won praise 39 · views 210 000 +

Guess you like

Origin blog.csdn.net/XiaoXiao_RenHe/article/details/103983376