Sunday, April 12, 2009

Run custom application or script as a Windows service

If you need your application or script to be started, whenever Windows is booted, you need to install it as a service (so  it will even be started when you didn’t login yet). Microsoft offers a little tool to easily create your own service and make it run some batch file / application as a part of their Windows Server Resource Kit Tools. From this package, you only need srvany.exe and instsrv.exe to install your custom service, but you still need to perform all this manual configuration before your service will be running.

I created the following batch script to easily install or uninstall your custom application as a service. All required information will be requested interactively to the user step by step. No further manual configuration will be needed anymore. Errors will be shown with colors and attention points will be flashed.

@echo off
echo.
echo.
echo Use this script to install / uninstall your custom applications or scripts as a service and make sure they will be started whenever Windows is booted.
SET DFAULT_BACKGROUND_COLOR=0
SET DEFAULT_TEXT_COLOR=7
SET ERROR_BACKGROUND_COLOR=4
SET ERROR_TEXT_COLOR=F
SET ATTENTION_BACKGROUND_COLOR=0
SET ATTENTION_TEXT_COLOR=F
rem 0 = Black
rem 1 = Blue
rem 2 = Green
rem 3 = Aqua
rem 4 = Red
rem 5 = Purple
rem 6 = Yellow
rem 7 = White
rem 8 = Gray
rem 9 = Light Blue
rem A = Light Green
rem B = Light Aqua
rem C = Light Red
rem D = Light Purple
rem E = Light Yellow
rem F = Bright White
:MAIN_MENU
  COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  SET RETURN_DRAW_ATTENTION_POINT=MAIN_MENU_INPUT
  GOTO DRAW_ATTENTION
  :MAIN_MENU_INPUT
    echo.
    echo.
    SET INPUT=
    echo To Install a new custom service, press I.
    echo To Uninstall a previously created custom service, press U.
    echo To open the Windows Services application, press S.
    set /p INPUT=Press E to Exit: 
    if /i "%INPUT%" == "i" goto START_INSTALL
    if /i "%INPUT%" == "u" goto START_UNINSTALL
    if /i "%INPUT%" == "s" goto START_SERVICES
    if /i "%INPUT%" == "e" goto EXIT
    GOTO MAIN_MENU
  :END_MAIN_MENU_INPUT
  echo.     
:END_MAIN_MENU
  
:START_INSTALL
  rem INTERACTIVE MODE is used, lines below kept for reference
  :HARD_CODED_PATHS_AND_NAMES
  rem APP_NAME=REPLACE_THIS_WITH_YOUR_CUSTOM_APP_NAME
  rem APP_PATH=FULL_PATH_TO_CUSTOM_APP
  rem one can use %CD% in the path, which will be replace by the current directory
  rem uncomment following 2 lines if you want to use the folder name (where this bat file is run) as service name APP_NAME
  rem SET APP_NAME=
  rem for %%* in (.) do set APP_NAME=%%~n*
  
  :INTERACTIVE_INPUT_OF_PATHS_AND_NAMES
    :INTERACTIVE_INPUT_APP_NAME
      echo.
      SET APP_NAME=
      set /p APP_NAME=Provide the name of your custom application (will be used as the name of the service): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_NAME
    
    :INTERACTIVE_INPUT_OF_APP_PATH
      echo. 
      SET APP_PATH=
      set /p APP_PATH=Provide the full path of your custom application to run as a service (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_OF_APP_PATH
    
    :CHECK_CUSTOM_APP_PATH
      IF EXIST "%APP_PATH%" GOTO END_CHECK_CUSTOM_APP_PATH
      echo.
      echo Your custom application %APP_NAME% could not be found at %APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_CUSTOM_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
      
    :INTERACTIVE_INPUT_APP_FOLDER
      echo.
      SET APP_FOLDER=
      set /p APP_FOLDER=Provide the folder to be used as working directory for your custom application (press ENTER if not required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
      
    :INTERACTIVE_INPUT_APP_PARAM
      echo.
      SET APP_PARAM=
      set /p APP_PARAM=Provide the parameters of your custom application (press ENTER if no parameters are required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
    
    :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      echo. 
      SET INST_SERVICE_APP_PATH=
      set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
    :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
    
    :CHECK_INST_SERVICE_APP_PATH_INPUT
      if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH
    :END_CHECK_INST_SERVICE_APP_PATH_INPUT
    
    :CHECK_INST_SERVICE_APP_PATH
      IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH
      echo.
      echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_INST_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    
    :INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      echo. 
      SET SERVICE_APP_PATH=
      set /p SERVICE_APP_PATH=Provide the full path to srvany.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\srvany.exe): 
    :END_INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
    
    :CHECK_SERVICE_APP_PATH_INPUT
      if "%SERVICE_APP_PATH%"=="" goto DEFAULT_SERVICE_APP_PATH
    :END_CHECK_SERVICE_APP_PATH_INPUT
    
    :CHECK_SERVICE_APP_PATH
      IF EXIST "%SERVICE_APP_PATH%" GOTO END_CHECK_SERVICE_APP_PATH
      echo.
      echo srvany.exe could not be found at %SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :END_INTERACTIVE_INPUT_OF_PATHS_AND_NAMES  
  
  :SHOW_CONFIG
    echo.
    echo Ready to install your custom application, using the following configuration:
    echo.
    echo   Custom application name: %APP_NAME%
    echo   Custom application full path: %APP_PATH%
    echo   Custom application parameters: %APP_PARAM%
    echo   Custom application working directory: %APP_FOLDER%
    echo   srvany.exe full path: %SERVICE_APP_PATH%
    echo   instsrv.exe full path: %INST_SERVICE_APP_PATH%
    echo.
    SET RETURN_DRAW_ATTENTION_POINT=SHOW_CONFIG_WAIT
    GOTO DRAW_ATTENTION
    :SHOW_CONFIG_WAIT
      COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
      set INPUT=
      set /p INPUT=Press ENTER to continue installation, all other will return to main menu: 
      if /i not "%INPUT%" == "" GOTO MAIN_MENU
      COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    :END_SHOW_CONFIG_WAIT
  :END_SHOW_CONFIG
  
  :INSTALL_SERVICE
    "%INST_SERVICE_APP_PATH%" "%APP_NAME%" "%SERVICE_APP_PATH%"
    echo.
    echo Service installed : %APP_NAME%
    echo.
  :END_INSTALL_SERVICE
  
  :UPDATE_REGISTRY
    REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "Application" /t REG_SZ /d "%APP_PATH%" /f
    IF NOT "%APP_FOLDER%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppDirectory" /t REG_SZ /d "%APP_FOLDER%" /f
    IF NOT "%APP_PARAM%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppParameters" /t REG_SZ /d "%APP_PARAM%" /f
    echo.
    echo Registry updated to run %APP_PATH% with service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY
  
  :START_SERVICE
    SET RETURN_DRAW_ATTENTION_POINT=START_SERVICE_INPUT_REQUEST
    GOTO DRAW_ATTENTION
    :START_SERVICE_INPUT_REQUEST
      echo The service is now installed successfully (if no errors are logged above!)
      set INPUT=
      set /p INPUT=To start the service now, press Y. All others will return to main menu: 
      if /i not "%INPUT%" == "y" goto END_START_SERVICE
    :END_START_SERVICE_INPUT_REQUEST
    NET START "%APP_NAME%"
    echo.
    echo Service %APP_NAME% started
  :END_START_SERVICE
  
  GOTO END_START_INSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_INST_SERVICE_APP_PATH
  
  :DEFAULT_SERVICE_APP_PATH
    echo Using default path for srvany.exe %SERVICE_APP_PATH%
    SET SERVICE_APP_PATH=%CD%\srvany.exe
    GOTO END_CHECK_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_SERVICE_APP_PATH
:END_START_INSTALL
  GOTO MAIN_MENU
:START_SERVICES
  start "Services" services.msc
:END_START_SERVICES
  GOTO MAIN_MENU
:START_UNINSTALL
  :INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    echo.
    SET APP_NAME=
    set /p APP_NAME=Provide the name of your custom application (the name of the service to uninstall): 
    echo. 
  :END_INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    
  :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    echo. 
    SET INST_SERVICE_APP_PATH=
    set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
  :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
    if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
  :END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    echo.
    echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
    echo Please try again!
    SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    GOTO DRAW_ERROR_ATTENTION
  :END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :STOP_SERVICE
    NET STOP %APP_NAME%
    echo.
    echo Service %APP_NAME% stopped
    echo.
  :END_STOP_SERVICE
  
  :REMOVE_SERVICE
    "%INST_SERVICE_APP_PATH%" %APP_NAME% REMOVE
    echo.
    echo Service %APP_NAME% removed
    echo.
  :END_REMOVE_SERVICE
  
  :UPDATE_REGISTRY_UNINSTALL
    REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\ /va /f
    echo.
    echo Registry cleaned up for service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY_UNINSTALL
  
  GOTO END_START_UNINSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  :END_DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
:END_START_UNINSTALL
  GOTO MAIN_MENU
  
:EXIT_WITH_PAUSE
  echo The end. Comments and updates: http://myTselection.blogspot.com, Copyright MightyMouse. Copyright srvany and instsrv Microsoft.
  pause
  GOTO EXIT
:END_EXIT_WITH_PAUSE
  
:EXIT
  exit
:END_EXIT
:DRAW_ATTENTION
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
:END_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ATTENTION_POINT%
:DRAW_ERROR_ATTENTION
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
:END_ERROR_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ERROR_ATTENTION_POINT%
@echo on


A full package to easily install your own custom application as a service can be downloaded here, sources (zip).



Update (26/04/09): Added possibility to specify application working directory and command line parameters. (AppDirectory and AppParameter registry keys.)

Update (09/10/2011): New links

No comments:

Post a Comment