26

これは単純なはずですが、私はそれを見ていません。

プロセス ID がある場合、それを使用してプロセス名などのプロセスに関する情報を取得するにはどうすればよいですか。

4

5 に答える 5

20

Linux では、proc ファイルシステムを読み取ることができます。ファイル/proc/<pid>/cmdlineにはコマンドラインが含まれています。

于 2010-11-15T23:07:24.297 に答える
15

PSUtil を試す -> https://github.com/giampaolo/psutil

Windows と Unix で問題なく動作することを思い出します。

于 2010-11-15T23:05:18.833 に答える
2

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)

同じ名前の 1 つのプログラムまたはすべてのプログラムが必要で、そのプロセスまたは何かを強制終了したい場合は、次のようにします。

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)

これは私のプロセス キル プログラムの貼り付けにすぎません。

于 2014-07-09T03:34:03.313 に答える