回答
在Linux下,您可以讀取proc文件系統。文件/proc/<pid>/cmdline
包含命令行。
另一種方法是'ps -o cmd =
而'/ proc/
嘗試PSUtil - >https://github.com/giampaolo/psutil
適用於Windows和Unix很好,我記得。
這似乎是正確的做法。它讓我感到它不在debian 5中。 – andyortlieb 2010-11-16 14:08:09
這是Debian的(常見)問題,而不是與psutil;) – 2011-06-23 22:01:18
你可以「python setup.py install」從源代碼安裝它。 – 2011-08-04 11:17:28
對於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)
這只是我的一個進程kill程序的貼我可以使它好多了,但它是好的。
試試這個
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
- 1. 通過PID查找進程名稱
- 2. 通過ssh獲取nohup進程的PID
- 3. 通過PID獲取進程句柄
- 4. 通過名稱獲取進程ID
- 5. 如何通過PID查找應用程序名稱(進程ID)
- 6. 從PID獲取名稱?
- 7. 通過圖像名稱獲取進程的進程句柄
- 8. 通過服務的名稱獲取Windows服務的PID
- 9. 如何從PID使用C獲取進程名稱
- 10. 如何從C++中的PID獲取進程名稱?
- 11. 如何使用PID從服務器層獲取進程名稱
- 12. 從pid或句柄獲取進程名稱
- 13. 獲取java進程的名稱而不是pid?
- 14. linux在內核中從pid獲取進程名稱
- 15. 如何通過在Mac OS X中提供進程名稱來獲取進程的PID?
- 16. 獲取名稱,:通過
- 17. linux - 獲取進程的pid
- 18. 從子進程獲取PID
- 19. SharpPcap獲取進程名稱
- 20. 通過Ruby中的pid獲取進程狀態
- 21. 通過Win7中的pid獲取進程信息
- 22. 獲取通過ssh使用sudo啓動的進程的PID
- 23. 獲取通過ssh使用nohup啓動的進程的PID
- 24. 來自pid的Unix進程名稱
- 25. Linux中的pid進程名稱
- 26. 從進程名稱獲取進程ID
- 27. 通過窗口句柄獲取未知進程的名稱?
- 28. Linux命令通過名稱獲取Tomcat進程ID
- 29. 如何通過Python獲取進程名稱?
- 30. 通過進程名稱取消隱藏進程?
的可能重複[你如何在python程序的進程id?](http://stackoverflow.com/questions/3761639/how-do-you-get python中的程序代碼) – 2014-07-23 15:58:17
@PeterO。這個問題是這個**的反向**。 – 2015-02-13 13:56:18