Finding the command for a specific PID in Linux from Python

linux, process, python

Solution

Using a /proc files has a disadvantage of lower portability, which may or may not be a concern. Here's how you can use the standard shell command for that

ps -w -w -p <YOUR PID> -o cmd h

Note the two `-w` options that instructs ps to not truncate the output (which it does by default)

Reading its output from python is as simple as calling a single function `subprocess.check_output()` documented here.

Problem

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.

Original source