3

I've scratched my head raw on this. Please help me stop the scratching.

We have a manual workaround for a site problem we're experiencing. The workaround always works when we do the steps manually, but running a script that does the same steps sometimes works but usually doesn't. My question: Why would there be a difference between performing the steps manually and performing them under script?

The manual steps are:

  1. Stop the site
    • in IIS Manager, select the site, click Stop
  2. Recycle the app pool the backend service runs in
    • in IIS Manager, select the application pool, click Recycle
  3. Make a call to a method on the backend service. This is to prepopulate some data before bringing the site up. The backend service is a .asmx
    • In Internet Explorer, navigate to the backend's method Invoke page, enter some parameters, click "Invoke"
  4. Start the site
    • in IIS Manager, select the site, click Start

As I said, this always brings the site up with full functionality. This should be so scriptable, right?

So I write a batch script that essentially does this:

%windir%\system32\inetsrv\appcmd.exe stop site nameOfSite
%windir%\system32\inetsrv\appcmd.exe recycle apppool nameOfAppPool
curl...(call web method with headers and parameters identical to what IE does)
%windir%\system32\inetsrv\appcmd.exe start site nameOfSite

The curl call appears identical in Fiddler to using IE to make the call. Further, I've replaced the curl call with a pause so the script gives me a chance to use IE to call the web method. So I'm pretty sure it's not that line.

I've put delays between the steps.

I've tried starting/stopping the appPool (instead of a recycle).

I've tried doing the same thing with Python:
subprocess.check_output([params to call appcmd], shell=True)

None of these attempts changes the behaviour:

  • each step reports success
  • sometimes the site is actually up and everything is fine
  • more often than not, the site goes back down after about a minute. This NEVER happens if I perform the steps through IIS and IE.
  • occassionally the final step does not succeed and the site remains down.

When the script fails, merely re-running the script never works. I have to perform the steps manually then all is well.

This makes no sense to me. Does it to you? Is there an alternate to appcmd that more closely does what IIS does in starting/stopping/recycling? What am I doing wrong?

biscuit314
  • 113
  • 9
  • Maybe you could enable logging to understand what is happening : `appcmd set config /section:httpLogging /dontLog:False` – krisFR May 10 '15 at 22:01
  • Are you certain the curl is actually executing the service method in the way it should? I don't know curl very well, but I'm betting your testing would have exposed any HTTP 500s by now, but not knowing the guts of your service, it's possible that that step is not actually doing what it's supposed to. – nateirvin May 14 '15 at 14:33
  • Have you tried replacing the appcmd statements with Powershell commands? https://technet.microsoft.com/en-us/library/ee790599.aspx I understand that figuring out what the problem is will provide a great sense of relief and accomplishemnt, and switching to Powershell might be a "workaround" instead of a solution, so I'm just making the suggestion in case you just want it to work right regardless. – Tony Hinkle May 14 '15 at 15:21
  • 1
    @nateirvin - That's where I had my doubts at first. But two things convince me that's not the problem: Fiddler traffic is identical with curl as with IE (request and response). Second, if I use IIS to stop/start the site and appPool but use curl to execute call the web method, it works. If I use appcmd to stop/start and IE to call the web method, it behaves as the script itself does. Not conclusive proof, but very compelling. – biscuit314 May 14 '15 at 19:52
  • Might help to do it manually on the command line before scripting it. Do a list before/after each command to make sure names/states are what is expected. – Brian May 14 '15 at 22:29
  • it sounds like timing - you are not checking to see one line of appcmd actually completes before launching the next. If its permissions it would always fail with a given runtime account - and you said this fails sometimes - so try what @Brian wrote above first. – David Nilson May 20 '15 at 21:11
  • @DavidNilson It does sound like timing and @BrianMay I have executed the command lines manually. `appcmd` appears to be synchronous: it prints "Successfully stopped" (etc) before the next line executes. JIC I inserted delays up to 10 seconds (way longer than between the steps when doing it manually). When I execute the commands line by line, I get the same results: sometimes works, usually doesn't. If I use IIS to start/stop, but the command line to call the web method - it always works. Only when I use `appcmd` does it not work. I didn't do list before/after, though... lemme try that. – biscuit314 May 20 '15 at 21:31
  • @TonyHinkle I'm not after elegant here, only reliable :-) I've only lightly tried the Powershell commands so far. They don't appear to provide the feedback that `appcmd` does (e.g. "...successfully started") I didn't check to see if they set `errorlevel` I'll keep playing with them. – biscuit314 May 20 '15 at 21:33
  • I know, old thread, but I have the exact same problem. A site that has problems after the server is rebooted. It will have all urls set to local host. Stopping and starting the app pool and then restarting the site fixes it when I'm the gui, but do the exact same in powershell or appcmd and it isn't fixed. Adding delays etc makes no difference. Running it with the same account, manually. Can do it multiple times with the script, doesn't work, but can see it stopping starting the app pool and restarting the site. Go on to the gui, do the same, works fine. Did you find a solution to this? – David Jul 19 '21 at 09:29
  • @David We never did find a solution. We ended up rewriting the app, no longer using IIS. – biscuit314 Jul 20 '21 at 13:33

1 Answers1

0

What are the permissions for the user account that is running the script.

I assume you are an Administrator, but is the script runtime account also an Administrator level account?

Check that....

David Nilson
  • 409
  • 2
  • 5
  • As you said in OP comments, if it was permissions it would always fail. – biscuit314 May 20 '15 at 21:20
  • agreed -- provided the script runtime account was the same each time - should there be more than one account involved - this could be worth looking at..... like running the script from CL, and then from a scheduler could involve multiple runtime accounts. Something else to check. – David Nilson May 20 '15 at 21:49