Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the command for a specific PID in Linux from 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.

like image 505
Evan Fosmark Avatar asked Sep 17 '09 19:09

Evan Fosmark


People also ask

How do I find the PID of a process in Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

What is the command used to check the pid?

ps command is used to list the currently running processes and their PIDs along with some other information depends on different options.


1 Answers

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.

like image 108
P Shved Avatar answered Oct 13 '22 00:10

P Shved