0

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

Here is my output per a answer

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

<appcmd>

    <WP WP.NAME="8476" APPPOOL.NAME="8476.com" />

    <WP WP.NAME="11636" APPPOOL.NAME="11636.com" />

    <WP WP.NAME="8868" APPPOOL.NAME="8868.com" />

    <WP WP.NAME="6180" APPPOOL.NAME="6180.com" />

    <WP WP.NAME="5636" APPPOOL.NAME="5636.com" />

    <WP WP.NAME="12616" APPPOOL.NAME="12616.com" />

    <WP WP.NAME="7472" APPPOOL.NAME="7472.com" />

    <WP WP.NAME="1668" APPPOOL.NAME="1668.com" />

    <WP WP.NAME="9608" APPPOOL.NAME="9608.com" />

    <WP WP.NAME="12480" APPPOOL.NAME="12480.com" />

</appcmd>
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 1
    Why the unnecessary laziness? Just add the `.exe` file extension. Without doing that your code will take slightly longer to execute, or not at all, because it relies upon `%PATHEXT%` being defined, and if so, each extension listed in its value tested, first to last order, until a matching one is met. Obviously, as you're using `appcmd` twice on that line, the small delay would be worsened. Please use `appcmd.exe` instead. – Compo Jul 25 '22 at 17:39

2 Answers2

1

EDIT 2022/08/04: New code based on new posted data

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

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

<appcmd>

    <WP data for pool 1 />

    <WP data for pool 2 />

    . . .

</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="
set "line1="
for /F "delims=" %%a in (appcmdXMLout.txt) do (

   if not defined header (
      set "header=%%a"
      setlocal EnableDelayedExpansion
      > appcmdXMLin.txt echo !header!
      endlocal
   ) else if not defined line1 (
      set "line1=%%a"
      setlocal EnableDelayedExpansion
      >> appcmdXMLin.txt echo !line1!
      endlocal
   ) else if "%%a" neq "</appcmd>" (
      rem One appcmd completed: process it
      (
      set /P "=%%a" < NUL
      echo/
      echo ^</appcmd^>
      ) >> appcmdXMLin.txt

      %windir%\system32\inetsrv\appcmd.exe recycle apppool /in < appcmdXMLin.txt
      timeout /T 5 > NUL

      setlocal EnableDelayedExpansion
      (
      echo !header!
      echo !line1!
      ) > appcmdXMLin.txt
      endlocal

   )

)

del appcmdXMLout.txt appcmdXMLin.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Where is the delay at? I ran this and it ran all at once for all 15 app pools? – Mike Flynn Aug 04 '22 at 13:17
  • Ok. Please, execute this command: `%windir%\system32\inetsrv\appcmd.exe list wp /xml > appcmdXMLout.txt` and post the output. To do this, [edit your question](https://stackoverflow.com/posts/73112982/edit) and post the output at the end. After that, post a comment here as an advice to me. If the file is too large, inspect it and try to identify the lines that delimit each one of the 15 pools, and post just one pool indicating the ending line. – Aacini Aug 04 '22 at 18:56
  • I modified the code accordingly to the new data. It should work correctly now... – Aacini Aug 05 '22 at 01:10
  • Looks like it is doing it every 5 seconds, awesome, thanks! – Mike Flynn Aug 05 '22 at 14:25
0

[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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • ...or, in addition to [my previous advice](https://stackoverflow.com/questions/73112983/recycle-all-app-pools-with-a-bat-file-with-a-delay-between-each-one#comment129129392_73112983) Mike Flynn, for the sake of not relying on _laziness_ to autofix unnecessary omissions, use `[space]1>NUL`. You should see by using the `1` handle, the reason why the above answer mentions the importance of the space. – Compo Jul 25 '22 at 18:01
  • Didnt work `The syntax of the command is incorrect. Failed to process input: Invalid XML input - please make sure that your XML is well-formed and follows the required format (HRESULT=c00cee3a).` – Mike Flynn Jul 25 '22 at 21:27
  • If the `/xml` option is not outputting a single line, or technically that which includes characters understood to be line terminators, then clearly `%%b` will not expand to fully formed `xml` to pipe into the seconary `appcmd.exe` command. – Compo Jul 25 '22 at 23:18
  • 1
    @Compo how about you just answer the question, or stop commenting. – Mike Flynn Jul 29 '22 at 12:49
  • Don't be rude @MikeFlynn, I'm answering the question you asked in your comment. Please note that Magoo specifically mentioned that their answer was 'Theoretical', you provided a error message, and I'm suggesting what may be causing it. If you don't want further help, you are certainly going the right way about achieving that. – Compo Jul 29 '22 at 16:42
  • 2
    I understand, but your other comments just aren't helping. You haven't helped once in all your comments. – Mike Flynn Jul 30 '22 at 14:31
  • You are wrong @MikeFlynn. My [first comment](https://stackoverflow.com/questions/73112983/recycle-all-app-pools-with-a-bat-file-with-a-delay-between-each-one#comment129129392_73112983) specifically provides you with an improvement to the speed and robustness of your submitted code. If you don't think that is providing help, then that is a reflection on you, not on my advice. My second comment explains that `>` is essentially unnecessary shorthand, for `1>`. Shorthand in not needed in a script which is only typed once. ```Echo I am number 1>CON``` does not print `I am number 1` to the console. – Compo Aug 03 '22 at 14:20