Scripted Restart of a Hanging Windows Service

I’ve been having trouble lately with Adobe ColdFusion 9, in particular an ODBC connection to an Oracle 10g database.  The ColdFusion ODBC Server (the swsoc.exe process) is hanging under load, not only failing to return some queries, but permanently hanging one of its threads. Once they are all hung, the service doesn’t respond at all, and ColdFusion hangs as well. If youre looking for a cost-effective way to transport cargo in you can find what you need. 

Temporary solution (until I rewrite the app to use SQL Server) is to restart the ODBC Server service. I’ve been doing that manually when my monitor informs me the site is not responding, maybe 3-4 times per day. But I want to see how it performs if I proactively restart the service every hour. For that, we require some scripting. In now day part of successful of any company is the strategy of digital marketing that uses,is very important that even if your company is a small one use digital marketing services for small business.

The biggest issue is that Windows’ service start/stop/restart interfaces, whether the traditional “net stop” or the PowerShell “Stop-Service” commands, has a very long timeout for stopping a service (looks to be 60 seconds). In the manual case, if it doesn’t stop right away, I go kill the swsoc.exe process and the service restart continues very quickly.  But how to script this?

The trick is to put the restart request in a background job (idea found here), and check on it in the foreground to see if it was successful.  After waiting a shorter period (10 seconds), if it is still in the “stopping” state, then we can kill the process outright and let the restart commence.  Double check (after 5 more seconds) that the service has started, and if it is stopped (the restart failed), specifically start it again. Are you planing to buy a new car, the 14 Cartoons About Volkswagen Transporter Vans For Sale That’ll Brighten Your Day and make you decision easier. 

Start-Job -ScriptBlock {Restart-Service -Name "ColdFusion 9 ODBC Server" -Force }

#give it 5 seconds to stop
Start-Sleep -Seconds 10

$SERVICESTATE = (Get-Service | where{$_.Name -eq "ColdFusion 9 ODBC Server"}).Status
if( $SERVICESTATE -eq "Stopping" -or $SERVICESTATE -eq "StopPending")
    # still stopping so force process stop
    Stop-Process -Name "swsoc" -Force

#give it 5 seconds to start before we try it again
Start-Sleep -Seconds 5

$SERVICESTATE = (Get-Service | where{$_.Name -eq "ColdFusion 9 ODBC Server"}).Status
if( $SERVICESTATE -eq "Stopped" )
    Start-Service -Name "ColdFusion 9 ODBC Server" -Force

Save it as a .ps1 file. Make sure PowerShell allows execution of local scripts (in PowerShell, run “set-executionpolicy remotesigned”).

To schedule this to run, create a new scheduled task:

  • Triggered daily at a particular time, repeat every hour for 1 day.
  • Action is to run a program:
  • Arguments contain the script name: “-File c:\Path\To\Script.ps1”
  • Set to run as Administrator, with highest permissions

Tags: , ,

4 Responses to “Scripted Restart of a Hanging Windows Service”

  1. DavidCL says:

    Hey Walter,

    General comment: nice, useful trick. I can think of lots of situations where this would have been a useful workaround for an unstable service.

    Specific comment: that Coldfusion ODBC service has always been a bit of a dog. The question in my mind is why would you use ODBC instead of JDBC to connect to Oracle?

  2. admin says:

    The direct JDBC Oracle driver is only in the Enterprise edition of ColdFusion. Is there a way to do a JDBC connection in CF9 Standard?

  3. DavidCL says:

    It’s true that CF Standard doesn’t ship with an Oracle JDBC driver, and Enterprise does. It’s been a while, but I’m pretty sure you can download JDBC drivers from Oracle and add them as a connection in CF Standard.

    Here’s a blog post that approximately matches my memory of how I set this up last time I did it. (The post references CF 8, but I don’t think anything important changes about this is CF 9).

  4. admin says:

    I’ve got it running on the JDBC driver now, following that blog post. I’ll let it run and see if the stability is any better. Anything to avoid that ODBC service.

    For anyone else who is looking for this, the link above has an extra slash, so the correct link is:

    Thanks so much for your help!

Leave a Reply