我知道它与 /proc 有关,但我不太熟悉它。
教科书的答案是使用像这样的
psutil
模块:
import psutil,getpass,os
user_name = getpass.getuser()
process_dict = {proc.pid:proc.name() for proc in psutil.process_iter() if proc.username() == user_name}
这会生成一个字典,其中进程 id 作为键,进程名称作为当前用户进程的值。
这种方法看起来不错,但在我的 Windows 服务器计算机上(我没有管理员权限),我无法在不出现
proc.username()
异常的情况下获得 psutil.AccessDenied
。所以我尝试在辅助例程中捕获异常,这导致我遇到了另一个奇怪的错误,所以我放弃了整个想法并基于 tasklist
命令构建了一个仅限 Windows 的解决方案:
tasklist /FI "USERNAME eq %USERNAME%" /FO CSV
其中,适应Python并具有相同方便的pid =>用户名字典格式翻译为:
import csv,subprocess
def get_current_user_processes():
csv_output = subprocess.check_output(["tasklist","/FI","USERNAME eq {}".format(os.getenv("USERNAME")),"/FO","CSV"]).decode("ascii","ignore")
cr = csv.reader(csv_output.splitlines())
next(cr) # skip title lines
return {int(row[1]):row[0] for row in cr}
它告诉
tasklist
仅保留当前用户进程,并使用 csv 输出格式,以便可以使用 csv
模块轻松解析。然后,我们只保留前 2 列(名称和 pid),并将 pid 转换为整数以进行良好测量。
它创建这样的字典:
{7808: 'conhost.exe', 12676: 'dwm.exe', 10120: 'ssonsvr.exe', 7052: 'tasklist.exe', 17028: 'ccshelserver.exe', 14876: 'wfcrun32.exe', 14364: 'Receiver.exe', 15024: 'conhost.exe'}
现在可以使用
psutil
更深入地检查每个单独的进程,而不会冒访问被拒绝或其他奇怪错误的风险:
d = get_current_user_processes()
processes = [proc for proc in psutil.process_iter() if proc.pid in d]
import subprocess
ps = subprocess.Popen('ps -ef', shell=True, stdout=subprocess.PIPE)
print ps.stdout.readlines()
您可以尝试使用psutil.process_iter()。例如:
import psutil
pids=[process.pid for process in psutil.process_iter() if
process.username == 'root']
在这种情况下,我假设您想找到“root”用户运行的每个进程
我在这种情况下使用以下内容,检查这对您的情况是否有帮助。目前它检查当前用户名,但您可以自定义它。
import psutil
processes = filter(lambda p: (p.username() == os.getlogin() and (p.name() == 'python')) , psutil.process_iter())
print(list(processes))
使用 psutil 几天后,我发现这是最好的跨平台解决方案:
import psutil
def iter_user_procs():
# get username of the current Python process.
# Using this instead of something like os.getlogin() avoids cross-platform issues,
# such as Windows using USERDOMAIN\USERNAME instead of just USERNAME for process owners.
username = psutil.Process.username() # get username in format used by processes
# get username from the info dict to avoid AccessDenied errors. See docs for iter_procs().
for p in psutils.iter_procs(['username']):
if p.info['username'] == username:
yield p
使用这里每个示例中由于各种原因对我不起作用的部分,我拼凑了以下内容:
import os
import psutil
pid=os.getpid()
print(f"pid: {pid}")
username = psutil.Process(pid).username()
print(f"username: {username}")
pids = []
try:
pids=[process.pid for process in psutil.process_iter() if process.username() == username]
except (psutil.AccessDenied, psutil.NoSuchProcess):
pass
print(f"pids: {pids}")
这似乎在 Ubuntu Linux 上的 Python 3.11.2 中工作得很好,并且不需要任何硬编码变量,如“用户名”或“pid”,而是动态检索这些变量。
popen 效果很好,因为您可以通过 grep、cut 等运行内容。因此您可以根据您的需要定制信息。