| Product(s): | APM Implementation and Performance Management | |
| Version(s): | R6.0+ | |
| Environment: | N\A | |
| Area: | N/A | |
| Subarea: | N\A | |
Below is a sample command file that can be used to programmatically shutdown all APM Server services on a server and then restart them.
Please set variables as appropriate and save the whole thing as a .bat file. This command file can be scheduled to run via Windows Task Scheduler. When scheduling the task, pick a time when you know there is no activity on the server, making sure that it does not coincide with any of your scheduled actions.
Solution
@echo off
@echo.
REM - IMPORTANT SECTION - VARIABLES TO BE FILLED OUT FOR YOUR ENVIRONMENT!!!!!!!!!!!!!!!!!!!!
REM - put the service names in the brackets below, separated by spaces
set SERVICE_LIST=(IvaraServerService1 IvaraServerService2...)
REM - set the location where you want the shutdown failure dump files to go
set SHUTDOWN_ERROR_DUMP_DIRECTORY="C:\ProgramData\IvaraLogs\dumps\Shutdown Failures"
REM - set the location of adplus.exe for taking dump files
set ADPLUS_EXE="C:\Program Files\Debugging Tools for Windows (x64)\adplus.exe"
set SECONDS_TO_WAIT_FOR_SERVICES_TO_STOP=600
REM - NOTE THAT THE FOLLOWING DELAY (SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES) HAPPENS TWICE because we create dump files twice (in case you are calculating total time needed)
set SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES=300
set SECONDS_TO_WAIT_BEFORE_TAKING_SECOND_DUMP_FILE=120
set SECONDS_TO_WAIT_FOR_TASKKILL_TO_COMPLETE=60
REM - END OF IMPORTANT SECTION - VARIABLES TO BE FILLED OUT FOR YOUR ENVIRONMENT!!!!!!!!!!!!!!!!!!!!
REM - DESCRIPTION OF STEPS TAKEN IN THIS BATCH FILE
REM - STEP 1: Stop all services in defined in SERVICE_LIST
REM - STEP 2: Wait %SECONDS_TO_WAIT_FOR_SERVICES_TO_STOP% seconds for all of the services to stop
REM - STEP 3: Check if any instances of IvaraServer.exe are still running
REM - SUBSEQUENT STEPS ONLY HAPPEN IF IVARASERVER FAILS TO SHUT DOWN
REM - STEP 4: Set all services in SERVICE_LIST to not restart automatically (so they won't restart after the taskkill)
REM - STEP 5: Take a dump file for any instances of IvaraServer that are still running
REM - STEP 6: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the dump files
REM - STEP 7: Wait %SECONDS_TO_WAIT_BEFORE_TAKING_SECOND_DUMP_FILE% before adplus takes a second dump file
REM - STEP 8: Take a second dump file for any instances of IvaraServer that are still running
REM - STEP 9: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the second dump file
REM - STEP 10: Taskkill any instances of IvaraServer.exe
REM - STEP 11: Wait %SECONDS_TO_WAIT_FOR_TASKKILL_TO_COMPLETE% before changing the service properties to restart (just to be safe so we don't get service restarting based on timing of taskkill)
REM - STEP 12: Reset all services in SERVICE_LIST to automatically restart 60 seconds after crashing
REM - STEP 13: Start all services in defined in SERVICE_LIST
REM - END DESCRIPTION OF STEPS TAKEN IN THIS BATCH FILE
REM - STEP 1: Stop all services in defined in SERVICE_LIST
@echo STEP 1: Stop all services in defined in SERVICE_LIST
for %%i in %SERVICE_LIST% do (
net stop %%i
)
REM - STEP 2: Wait %SECONDS_TO_WAIT_FOR_SERVICES_TO_STOP% seconds for all of the services to stop
@echo STEP 2: Wait %SECONDS_TO_WAIT_FOR_SERVICES_TO_STOP% seconds for all of the services to stop
timeout /t %SECONDS_TO_WAIT_FOR_SERVICES_TO_STOP%
@echo.
REM - STEP 3: Check if any instances of IvaraServer.exe are still running
@echo STEP 3: Check if any instances of IvaraServer.exe are still running
tasklist /nh /fi "imagename eq ivaraserver.exe" | find /i "ivaraserver.exe" >nul && (
@echo There is at least one IvaraServer still runnning.
) || (
@echo.
@echo There are no IvaraServer instances still runnning. Done.
GOTO :RESTART
)
REM - If we get in here, IvaraServer.exe is still running
@echo There is at least one instance of IvaraServer.exe still running after attempting to stop all of the services
REM - STEP 4: Set all services in SERVICE_LIST to not restart automatically (so they won't restart after the taskkill)
@echo STEP 4: Set all services in SERVICE_LIST to not restart automatically (so they won't restart after the taskkill)
for %%i in %SERVICE_LIST% do (
@echo Setting service %%i to no longer restart automatically
sc failure %%i reset= 500 actions= ""
@echo.
)
REM - create the directory %SHUTDOWN_ERROR_DUMP_DIRECTORY% (in case it does not exist)
mkdir %SHUTDOWN_ERROR_DUMP_DIRECTORY%
REM - STEP 5: Take a dump file for any instances of IvaraServer that are still running
@echo STEP 5: Take a dump file for any instances of IvaraServer that are still running
%ADPLUS_EXE% -hang -pn IvaraServer.exe -o %SHUTDOWN_ERROR_DUMP_DIRECTORY%
@echo.
REM - STEP 6: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the dump files
@echo STEP 6: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the dump files
timeout /t %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES%
@echo.
REM - STEP 7: Wait %SECONDS_TO_WAIT_BEFORE_TAKING_SECOND_DUMP_FILE% before adplus takes a second dump file
@echo STEP 7: Wait %SECONDS_TO_WAIT_BEFORE_TAKING_SECOND_DUMP_FILE% before adplus takes a second dump file
timeout /t %SECONDS_TO_WAIT_BEFORE_TAKING_SECOND_DUMP_FILE%
@echo.
REM - STEP 8: Take a second dump file for any instances of IvaraServer that are still running
@echo STEP 8: Take a second dump file for any instances of IvaraServer that are still running
%ADPLUS_EXE% -hang -pn IvaraServer.exe -o %SHUTDOWN_ERROR_DUMP_DIRECTORY%
@echo.
REM - STEP 9: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the second dump file
@echo STEP 9: Wait %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES% to allow adplus to finish creating the second dump file
timeout /t %SECONDS_TO_WAIT_FOR_ADPLUS_TO_CREATE_DUMP_FILES%
@echo.
REM - STEP 10: Taskkill any instances of IvaraServer.exe
@echo STEP 10: Taskkill any instances of IvaraServer.exe
taskkill /f /im IvaraServer.exe
@echo.
REM - STEP 11: Wait %SECONDS_TO_WAIT_FOR_TASKKILL_TO_COMPLETE% before changing the service properties to restart (just to be safe so we don't get service restarting based on timing of taskkill)
@echo STEP 11: Wait %SECONDS_TO_WAIT_FOR_TASKKILL_TO_COMPLETE% before changing the service properties to restart (just to be safe so we don't get service restarting based on timing of taskkill)
timeout /t %SECONDS_TO_WAIT_FOR_TASKKILL_TO_COMPLETE%
@echo.
REM - STEP 12: Reset all services in SERVICE_LIST to automatically restart 60 seconds after crashing
@echo - STEP 12: Reset all services in SERVICE_LIST to automatically restart 60 seconds after crashing
for %%i in %SERVICE_LIST% do (
@echo Setting service %%i to restart automatically 60 seconds after crashing
sc failure %%i reset= 500 actions= "restart/60000/restart/60000/restart/60000"
@echo.
)
:RESTART
REM - STEP 13: Start all services in defined in SERVICE_LIST
@echo STEP 13: Start all services in defined in SERVICE_LIST
for %%i in %SERVICE_LIST% do (
net start %%i
)
See Also
| Original Author: | Giselle Crawford | |