简体   繁体   English

找出Windows服务的运行进程名称.NET 1.1

[英]Finding out Windows service's running process name .NET 1.1

We are using a badly written windows service, which will hang when we are trying to Stop it from code. 我们正在使用一个写得很糟糕的Windows服务,当我们尝试从代码中阻止它时,它将挂起。 So we need to find which process is related to that service and kill it. 因此,我们需要找到与该服务相关的进程并将其终止。 Any suggestions? 有什么建议?

You can use System.Management.MangementObjectSearcher to get the process ID of a service and System.Diagnostics.Process to get the corresponding Process instance and kill it. 您可以使用System.Management.MangementObjectSearcher获取服务的进程ID,并使用System.Diagnostics.Process获取相应的Process实例并将其终止。

The KillService() method in the following program shows how to do this: 以下程序中的KillService()方法显示了如何执行此操作:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Management;

namespace KillProcessApp {
    class Program {
        static void Main(string[] args) {
            KillService("YourServiceName");
        }

        static void KillService(string serviceName) {
            string query = string.Format(
                "SELECT ProcessId FROM Win32_Service WHERE Name='{0}'", 
                serviceName);
            ManagementObjectSearcher searcher = 
                new ManagementObjectSearcher(query);
            foreach (ManagementObject obj in searcher.Get()) {
                uint processId = (uint) obj["ProcessId"];
                Process process = null;
                try
                {
                    process = Process.GetProcessById((int)processId);
                }
                catch (ArgumentException)
                {
                    // Thrown if the process specified by processId
                    // is no longer running.
                }
                try
                {
                    if (process != null) 
                    {
                        process.Kill();
                    }
                }
                catch (Win32Exception)
                {
                    // Thrown if process is already terminating,
                    // the process is a Win16 exe or the process
                    // could not be terminated.
                }
                catch (InvalidOperationException)
                {
                    // Thrown if the process has already terminated.
                }
            }
        }
    }
}

WMI has this information: the Win32_Service class. WMI具有以下信息:Win32_Service类。

A WQL query like 像WQL一样的查询

SELECT ProcessId FROM Win32_Service WHERE Name='MyServiceName'

using System.Management should do the trick. 使用System.Management应该可以解决问题。

From a quick look see: taskllist.exe /svc and other tools from the command line. 快速查看: taskllist.exe /svc和命令行中的其他工具。

You can use 您可以使用

tasklist /svc /fi "SERVICES eq YourServiceName"

To find the process name and id, and also if the same process hosts other services. 查找进程名称和ID,以及同一进程是否托管其他服务。

To answer exactly to my question - how to find Process related to some service: 要完全回答我的问题 - 如何找到与某些服务相关的流程:

ManagementObjectSearcher searcher = new ManagementObjectSearcher
  ("SELECT * FROM Win32_Service WHERE DisplayName = '" + serviceName + "'");

foreach( ManagementObject result in searcher.Get() )
{
  if (result["DisplayName"].ToString().ToLower().Equals(serviceName.ToLower()))
  {
    int iPID = Convert.ToInt32( result["ProcessId"] );
    KillProcessByID(iPID, 1000); //some method that will kill Process for given PID and timeout. this should be trivial
  }
}

} }

Microsoft/SysInternals has a command-line tool called PsKill that allows you to kill a process by name. Microsoft / SysInternals有一个名为PsKill的命令行工具,允许您按名称终止进程。 This tool also allows you to kill processes on other servers. 此工具还允许您终止其他服务器上的进程。 Windows SysInternals Windows SysInternals

Usage: pskill [-t] [\\computer [-u username [-p password]]] <process ID | 用法:pskill [-t] [\\ computer [-u username [-p password]]] <进程ID | name> 名>
-t Kill the process and its descendants. -t杀死进程及其后代。
-u Specifies optional user name for login to remote computer. -u指定登录到远程计算机的可选用户名。
-p Specifies optional password for user name. -p指定用户名的可选密码。 If you omit this you will be prompted to enter a hidden password. 如果省略此项,系统将提示您输入隐藏密码。

I guess it's a two step process - if it's always the same service, you can easily find the process name using methods suggested in other answers. 我想这是一个两步过程 - 如果它始终是相同的服务,您可以使用其他答案中建议的方法轻松找到过程名称。

I then have the following code in a class on a .NET 1.1 web server: 然后,我在.NET 1.1 Web服务器上的类中有以下代码:

Process[] runningProcs = 
          Process.GetProcessesByName("ProcessName");

foreach (Process runningProc in runningProcs)
{
    // NOTE: Kill only works for local processes
    runningProc.Kill();
}

The Kill method can throw a few exceptions that you should consider catching - especially the Win32Exception, that is thrown if the process cannot be killed. Kill方法可以抛出一些你应该考虑捕获的异常 - 尤其是Win32Exception,如果进程无法被杀死则抛出。

Note that the WaitForExit method and HasExited property also exist in the 1.1 world, but aren't mentioned on the documentation page for Kill in 1.1. 请注意, WaitForExit方法HasExited属性也存在于1.1世界中,但在杀戮1.1的文档页面中未提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM