{"id":624,"date":"2011-02-10T19:58:53","date_gmt":"2011-02-11T00:58:53","guid":{"rendered":"http:\/\/swildow.darktech.org\/wp\/?p=624"},"modified":"2011-02-10T19:58:53","modified_gmt":"2011-02-11T00:58:53","slug":"killing-a-process-that-refuses-to-die","status":"publish","type":"post","link":"http:\/\/www.wildow.com\/blog\/?p=624","title":{"rendered":"killing a process that refuses to die"},"content":{"rendered":"<p><a href=\"http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2004\/10\/16\/438.aspx\" target=\"_blank\">http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2004\/10\/16\/438.aspx<\/a><\/p>\n<h2>Killing a Windows Service that seems to hang on &#8220;Stopping&#8221;<\/h2>\n<p><span style=\"font-family: Arial; font-size: x-small;\">It sometimes happens (and  it&#8217;s not a good sign most of the time): you&#8217;d like to stop a Windows  Service, and when you issue the stop command through the SCM (Service  Control Manager) or by using the ServiceProcess classes in the .NET  Framework or by other means (net stop, Win32 API), the service remains  in the state of &#8220;stopping&#8221; and never reaches the stopped phase. It&#8217;s  pretty simple to simulate this behavior by creating a Windows Service in  C# (or any .NET language whatsoever) and adding an infinite loop in the  Stop method. The only way to stop the service is by killing the process  then. However, sometimes it&#8217;s not clear what the process name or ID is  (e.g. when you&#8217;re running a service hosting application that can cope  with multiple instances such as SQL Server Notification Services). The  way to do it is as follows:<\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: x-small;\"><!--more--><br \/>\n<\/span><\/p>\n<ol>\n<li><span style=\"font-family: Arial; font-size: x-small;\">Go to the command-prompt and query the service (e.g. the SMTP service) by using sc:\n<p>sc queryex SMTPSvc<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: Arial; font-size: x-small;\">This will give you the following information:\n<p><span style=\"font-family: Courier New;\">SERVICE_NAME: SMTPSvc<br \/>\nTYPE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 20\u00a0 WIN32_SHARE_PROCESS<br \/>\nSTATE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 4\u00a0 RUNNING<br \/>\n(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)<br \/>\nWIN32_EXIT_CODE\u00a0\u00a0\u00a0 : 0\u00a0 (0x0)<br \/>\nSERVICE_EXIT_CODE\u00a0 : 0\u00a0 (0x0)<br \/>\nCHECKPOINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nWAIT_HINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nPID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 388<br \/>\nFLAGS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :<\/span><\/p>\n<p>or something like this (the &#8220;state&#8221; will mention stopping).<br \/>\n<\/span><\/li>\n<li><span style=\"font-family: Arial; font-size: x-small;\">Over here you can find the process  identifier (PID), so it&#8217;s pretty easy to kill the associated process  either by using the task manager or by using taskkill:\n<p>taskkill \/PID 388 \/F<\/p>\n<p>where the \/F flag is needed to force the process kill (first try without the flag).<\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Arial; font-size: x-small;\">Please be careful when you do this; it&#8217;s  useful for emergencies but you shouldn&#8217;t use it on a regular basis (use  it as a last chance to solve the problem or to avoid the need of a  reboot in an exceptional situation). It can even be used to stop a  service that has the &#8220;NOT-STOPPABLE&#8221; and\/or\u00a0&#8220;IGNORES_SHUTDOWN&#8221; flag set  (e.g. Terminal Services on a Windows Server 2003 is non-stoppable), at  least when it&#8217;s not hosted in the system process. You can query all this  information by means of the sc command.<\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: x-small;\">For real freaks (<span style=\"text-decoration: underline;\"><strong>don&#8217;t do this on a production machine!<\/strong><\/span>):  if you want to show the behavior of the &#8220;Blaster&#8221; worm which caused the  RPC service to stop, try to stop the RPC service (but safe your work  first :-)). It&#8217;s pretty simple to do if you have administrative  privileges (just a great example of why you should NOT run as a  high-privileged user on the system). When you succeed in killing the  process (pretty straightforward), you&#8217;ll see the shutdown countdown  popping up (if you&#8217;ve seen Blaster in action in the past, you&#8217;ll have a  deja-vu). You can stop this by typing the command shutdown -a (abort  shutdown), as I posted previously in the Blaster-timeframe since this  wasn&#8217;t known very well and it was quite useful to abort the started  shutdown in order to apply the patch. You can even restart the service  then by using sc again. Notice that if the RPC is stopped, you can&#8217;t  even connect to the MMC console for the Services management  (services.msc) since this relies on RPC. So, you really can&#8217;t start the  service again by using the MMC snap-in. The only way to start the  service again is by using sc start &lt;servicename&gt;. The output of  this (nice but at the same time ugly) demo looks like this (<strong><span style=\"color: #ff0000;\">again, don&#8217;t try this at home; I&#8217;m not responsible for any damage or data loss possible<\/span><\/strong>):<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">C:\\Documents and Settings\\Administrator&gt;sc queryex rpcss<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">SERVICE_NAME: rpcss<br \/>\nTYPE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 20\u00a0 WIN32_SHARE_PROCESS<br \/>\nSTATE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 4\u00a0 RUNNING<br \/>\n(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN))<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\"> WIN32_EXIT_CODE\u00a0\u00a0\u00a0 : 0\u00a0 (0x0)<br \/>\nSERVICE_EXIT_CODE\u00a0 : 0\u00a0 (0x0)<br \/>\nCHECKPOINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nWAIT_HINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nPID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2332<br \/>\nFLAGS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">C:\\Documents and Settings\\Administrator&gt;taskkill \/pid 2332 \/F<br \/>\n<\/span><span style=\"font-family: Courier New; font-size: x-small;\">SUCCESS: The process with PID 2332 has been terminated.<\/p>\n<p>C:\\Documents and Settings\\Administrator&gt;shutdown \/a<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">C:\\Documents and Settings\\Administrator&gt;sc start rpcss<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">SERVICE_NAME: rpcss<br \/>\nTYPE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 20\u00a0 WIN32_SHARE_PROCESS<br \/>\nSTATE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2\u00a0 START_PENDING<br \/>\n(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN))<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\"> WIN32_EXIT_CODE\u00a0\u00a0\u00a0 : 0\u00a0 (0x0)<br \/>\nSERVICE_EXIT_CODE\u00a0 : 0\u00a0 (0x0)<br \/>\nCHECKPOINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nWAIT_HINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x7d0<br \/>\nPID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2520<br \/>\nFLAGS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">C:\\Documents and Settings\\Administrator&gt;sc queryex rpcss<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\">SERVICE_NAME: rpcss<br \/>\nTYPE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 20\u00a0 WIN32_SHARE_PROCESS<br \/>\nSTATE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 4\u00a0 RUNNING<br \/>\n(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN))<\/span><\/p>\n<p><span style=\"font-family: Courier New; font-size: x-small;\"> WIN32_EXIT_CODE\u00a0\u00a0\u00a0 : 0\u00a0 (0x0)<br \/>\nSERVICE_EXIT_CODE\u00a0 : 0\u00a0 (0x0)<br \/>\nCHECKPOINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nWAIT_HINT\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 0x0<br \/>\nPID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2520<br \/>\nFLAGS\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :<\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: x-small;\">If you decide to try it (ignoring my  warnings), don&#8217;t rely on the system afterwards since various  applications will have suffered from this. I&#8217;m experimenting with these  things myself only on a Virtual PC with undo disks enabled.<\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: x-small;\">To go short: sc is one of my favorite  commands to mess around with services (install, uninstall, etc) and to  query for information on services. The sc command can also be used to  query all the active drivers on the system. If you don&#8217;t like  system-beeps for example, you can use sc stop Beep to stop the according  driver. But please again, be careful when you play with it.  Fortunately, disastrous driver stoppings are not possible and will be  denied by sc.<\/span><\/p>\n<p><span style=\"font-family: Arial; font-size: x-small;\">More info on sc.exe can be found via <a href=\"http:\/\/www.microsoft.com\/resources\/documentation\/windows\/xp\/all\/proddocs\/en-us\/sc.mspx\">http:\/\/www.microsoft.com\/resources\/documentation\/windows\/xp\/all\/proddocs\/en-us\/sc.mspx<\/a>.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2004\/10\/16\/438.aspx Killing a Windows Service that seems to hang on &#8220;Stopping&#8221; It sometimes happens (and it&#8217;s not a good sign most of the time): you&#8217;d like to stop a Windows Service, and when you issue the stop command through the &#8230; <a class=\"more-link\" href=\"http:\/\/www.wildow.com\/blog\/?p=624\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/624"}],"collection":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=624"}],"version-history":[{"count":1,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":625,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions\/625"}],"wp:attachment":[{"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}