代码之家  ›  专栏  ›  技术社区  ›  andyortlieb

按PID获取进程名

  •  25
  • andyortlieb  · 技术社区  · 14 年前

    这应该很简单,但我没看到。

    如果我有一个进程ID,我如何使用它来获取进程的信息,比如进程名。

    5 回复  |  直到 14 年前
        1
  •  20
  •   Juho Östman    14 年前

    在Linux下,可以读取proc文件系统。文件 /proc/<pid>/cmdline 包含命令行。

        2
  •  14
  •   Giampaolo Rodolà    10 年前

    尝试PSUtil-> https://github.com/giampaolo/psutil

    我记得在Windows和Unix上运行得很好。

        3
  •  1
  •   user3818650    11 年前

    对于Windows

    在不下载任何模块的情况下获取计算机上所有程序的PID的方法:

    import os
    
    pids = []
    a = os.popen("tasklist").readlines()
    for x in a:
          try:
             pids.append(int(x[29:34]))
          except:
               pass
    for each in pids:
             print(each)
    

    如果您只想要一个或所有同名的程序,并且您想要终止进程或其他操作:

    import os, sys, win32api
    
    tasklistrl = os.popen("tasklist").readlines()
    tasklistr = os.popen("tasklist").read()
    
    print(tasklistr)
    
    def kill(process):
         process_exists_forsure = False
         gotpid = False
         for examine in tasklistrl:
                if process == examine[0:len(process)]:
                    process_exists_forsure = True
         if process_exists_forsure:
             print("That process exists.")
         else:
            print("That process does not exist.")
            raw_input()
            sys.exit()
         for getpid in tasklistrl:
             if process == getpid[0:len(process)]:
                    pid = int(getpid[29:34])
                    gotpid = True
                    try:
                      handle = win32api.OpenProcess(1, False, pid)
                      win32api.TerminateProcess(handle, 0)
                      win32api.CloseHandle(handle)
                      print("Successfully killed process %s on pid %d." % (getpid[0:len(prompt)], pid))
                    except win32api.error as err:
                      print(err)
                      raw_input()
                      sys.exit()
        if not gotpid:
           print("Could not get process pid.")
           raw_input()
           sys.exit()
    
       raw_input()
       sys.exit()
    
    prompt = raw_input("Which process would you like to kill? ")
    kill(prompt)
    

    这只是我的进程终止程序的一个粘贴,我可以使它更好,但它是好的。

        4
  •  1
  •   KBill    6 年前

    使用psutil,下面是我可以给您的最简单的代码:

    import psutil
    
    # The PID ID of the process needed
    pid_id = 1216
    
    # Informations of the Process with the PID ID
    process_pid = psutil.Process(pid_id)
    print(process_pid)
    # Gives You PID ID, name and started date
    # psutil.Process(pid=1216, name='ATKOSD2.exe', started='21:38:05')
    
    # Name of the process
    process_name = process_pid.name()
    
        5
  •  0
  •   Goblinhack    9 年前

    试试这个

    def filter_non_printable(str):
        ret=""
        for c in str:
            if ord(c) > 31 or ord(c) == 9:
                ret += c
            else:
                ret += " "
        return ret
    
    #
    # Get /proc/<cpu>/cmdline information
    #
    def pid_name(self, pid):
        try:
            with open(os.path.join('/proc/', pid, 'cmdline'), 'r') as pidfile:
                return filter_non_printable(pidfile.readline())
    
        except Exception:
            pass
            return