{"id":251,"date":"2012-11-15T11:23:06","date_gmt":"2012-11-15T16:23:06","guid":{"rendered":"http:\/\/walt.therices.org\/?p=251"},"modified":"2012-11-15T11:23:06","modified_gmt":"2012-11-15T16:23:06","slug":"scripted-restart-of-a-hanging-windows-service","status":"publish","type":"post","link":"http:\/\/www.r2infosys.com\/wp\/2012\/11\/scripted-restart-of-a-hanging-windows-service\/","title":{"rendered":"Scripted Restart of a Hanging Windows Service"},"content":{"rendered":"<p>I&#8217;ve been having trouble lately with Adobe ColdFusion 9, in particular an ODBC connection to an Oracle 10g database. \u00a0The 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&#8217;t respond at all, and ColdFusion hangs as well.<\/p>\n<p>Temporary solution (until I rewrite the app to use SQL Server) is to restart the ODBC Server service. I&#8217;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.<\/p>\n<p>The biggest issue is that Windows&#8217; service start\/stop\/restart interfaces, whether the traditional &#8220;net stop&#8221; or the PowerShell &#8220;Stop-Service&#8221; commands, has a very long timeout for stopping a service (looks to be 60 seconds). In the manual case, if it doesn&#8217;t stop right away, I go kill the swsoc.exe process and the service restart continues very quickly. \u00a0But how to script this?<\/p>\n<p>The trick is to put the restart request in a background job (idea found <a href=\"http:\/\/www.powershellcommunity.org\/Forums\/tabid\/54\/aft\/5243\/Default.aspx\">here<\/a>), and check on it in the foreground to see if it was successful. \u00a0After waiting a shorter period (10 seconds), if it is still in the &#8220;stopping&#8221; state, then we can kill the process outright and let the restart commence. \u00a0Double check (after 5 more seconds) that the service has started, and if it is stopped (the restart failed), specifically start it again.<\/p>\n<pre>\nStart-Job -ScriptBlock {Restart-Service -Name \"ColdFusion 9 ODBC Server\" -Force }\n\n#give it 5 seconds to stop\nStart-Sleep -Seconds 10\n\n$SERVICESTATE = (Get-Service | where{$_.Name -eq \"ColdFusion 9 ODBC Server\"}).Status\nif( $SERVICESTATE -eq \"Stopping\" -or $SERVICESTATE -eq \"StopPending\")\n{\n    # still stopping so force process stop\n    Stop-Process -Name \"swsoc\" -Force\n} \n\n#give it 5 seconds to start before we try it again\nStart-Sleep -Seconds 5\n\n$SERVICESTATE = (Get-Service | where{$_.Name -eq \"ColdFusion 9 ODBC Server\"}).Status\nif( $SERVICESTATE -eq \"Stopped\" )\n{\n    Start-Service -Name \"ColdFusion 9 ODBC Server\" -Force\n}\n<\/pre>\n<p>Save it as a .ps1 file. Make sure PowerShell allows execution of local scripts (in PowerShell, run &#8220;set-executionpolicy remotesigned&#8221;).<\/p>\n<p>To schedule this to run, create a new scheduled task:<\/p>\n<ul>\n<li>Triggered daily at a particular time, repeat every hour for 1 day.<\/li>\n<li>Action is to run a program: <br \/> &#8220;C:WindowsSystem32WindowsPowerShellv1.0powershell.exe&#8221;<\/li>\n<li>Arguments contain the script name: &#8220;-File c:PathToScript.ps1&#8221;<\/li>\n<li>Set to run as Administrator, with highest permissions<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been having trouble lately with Adobe ColdFusion 9, in particular an ODBC connection to an Oracle 10g database. \u00a0The 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&#8217;t respond at all, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[2],"tags":[7,27,26],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p74jni-43","_links":{"self":[{"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/posts\/251"}],"collection":[{"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.r2infosys.com\/wp\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}