Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the process name by pid in Linux using Python?

Tags:

python

linux

I want to get the process name, given it's pid in python. Is there any direct method in python?

like image 488
Mahsa Avatar asked Aug 30 '15 10:08

Mahsa


People also ask

How do I find the PID process name?

In this quick article, we've explored how to get the name and the command line of a given PID in the Linux command line. The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

How can we find the process name from its process ID in Linux?

With Linux, the info is in the /proc filesystem. To get the command line for process id 9999, read the file /proc/9999/cmdline . And to get the process name for process id 9999, read the file /proc/9999/comm .

How do I find out what processes are running on Python Linux?

wait() return output # This is the function you can use def isThisRunning( process_name ): output = findThisProcess( process_name ) if re.search('path/of/process'+process_name, output) is None: return False else: return True # Example of how to use if isThisRunning('some_process') == False: print("Not running") else: ...


1 Answers

The psutil package makes this very easy.

import psutil

process = psutil.Process(pid)

process_name = process.name()
like image 160
Cyphase Avatar answered Sep 22 '22 08:09

Cyphase