从Python查找Linux中特定PID的命令 [英] Finding the command for a specific PID in Linux from Python

查看:98
本文介绍了从Python查找Linux中特定PID的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能找出PID设置为的命令".当我说命令时,我的意思是当您在linux shell中运行命令"top"时在最后一栏中看到的内容.当我具有特定的PID时,我想以某种方式从Python获取此信息.

I'd like to know if it's possible to find out the "command" that a PID is set to. When I say command, I mean what you see in the last column when you run the command "top" in a linux shell. I'd like to get this information from Python somehow when I have a specific PID.

任何帮助都会很棒.谢谢.

Any help would be great. Thanks.

推荐答案

请不要在生产代码中使用/proc文件系统.相反,请使用定义明确的POSIX接口,例如glibc调用和标准的shell命令!确实需要使Linux世界更加标准化!

PLEASE, do not use /proc filesystem in production code. Instead, use well-defined POSIX interfaces, such as glibc calls and standard shell commands! Make the Linux world more standardized, it really needs to!

通过调用shell命令可以很好地实现

What you need is well achieved by invoking a shell command

ps -p <YOUR PID> -o cmd h

无需解析!

更不用说从python读取shell命令输出要比从/proc中的文件读取更多的工作.而且,这也使您的程序更易于移植!

Let alone that reading shell command output from python takes no more effort than reading from a file in /proc. And this makes your program more portable, either!

这篇关于从Python查找Linux中特定PID的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆