Home > database >  Recycle all app pools with a .bat file with a delay between each one
Recycle all app pools with a .bat file with a delay between each one

Time:08-02

I would like to have a delay between each recycled app pool so the CPU doesn't get high. Below is my current .bat file that recycles all my app pools at once. How can I add a delay between each one before the other gets executed?

%windir%\system32\inetsrv\appcmd list wp /xml | %windir%\system32\inetsrv\appcmd recycle apppool /in

CodePudding user response:

[Theoretical]

for /f "delims=" %%b in ('%windir%\system32\inetsrv\appcmd list wp /xml') do echo %%b|%windir%\system32\inetsrv\appcmd recycle apppool /in&timeout /t 3 >nul

I'm assuming that this is in a batch file. If running from the prompt, change each %%b to %b.

The >nul suppresses timeout's countdown. The 3 means 3 seconds and the space between 3 and > is required.

%%b should be set to each line appearing from the single-quoted command, and then gets echoed to the recycling app, followed by a delay of (an integral number) of seconds.

CodePudding user response:

The appcmd list wp /xml command outputs one XML file that contains several "appcmd" sections, one for each app pool, in this format:

<?xml version="1.0" encoding="UTF-8"?>

<appcmd>
    <data for app pool 1 here />
</appcmd>

<appcmd>
    <data for app pool 2 here />
</appcmd>

In this way, in order to execute each app pool cmd individually, we need to create individual well-formatted XML files. The Batch file below do so:

@echo off
setlocal DisableDelayedExpansion

rem Create the "all apps" XML output file
%windir%\system32\inetsrv\appcmd.exe list wp /xml > appcmdXMLout.txt

rem Separate "all apps" output file into individual app input file(s) and process each
set "header="
for /F "delims=" %%a in (appcmdXMLout.txt) do (

   if not defined header (
      set "header=%%a"
      setlocal EnableDelayedExpansion
      > appcmdXMLin.txt echo !header!
      endlocal
   ) else (
      (set /P "=%%a" < NUL & echo/) >> appcmdXMLin.txt
      if "%%a" equ "</appcmd>" (

         rem One appcmd completed: process it
         %windir%\system32\inetsrv\appcmd.exe recycle apppool /in < appcmdXMLin.txt
         setlocal EnableDelayedExpansion
         > appcmdXMLin.txt echo !header!
         endlocal
         timeout /T 5 > NUL

      )
   )

)

del appcmdXMLout.txt appcmdXMLin.txt
  • Related