Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking process status from Python

Tags:

python

linux

I am running on a Linux x86-64 system. From a Python (2.6) script, I wish to periodically check whether a given process (identified by pid) has become "defunct"/zombie (this means that entry in the process table exists but the process is doing nothing). It would be also good to know how much CPU the process is consuming (similar to what 'top' command shows).

Can somebody give me some pointers on how I can get these in Python?

like image 665
Arka Avatar asked Mar 11 '13 18:03

Arka


People also ask

How do you find the process status in Python?

You could use a the status feature from psutil: import psutil p = psutil. Process(the_pid_you_want) if p. status == psutil.

How do you check a process is running in Windows using Python?

We import the psutil module. Then we search for chrome.exe in all running processes on the local machine using psutil. process_iter(). If found it will return output as TRUE, else FALSE.

How do you check a process is running in Linux using Python?

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: ...

How do I list running processes in Python?

We would be using the WMI. Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI. Win32_Process() to get the running processes, iterated through each process and stored in variable process.


1 Answers

I'd use the psutil library:

import psutil

proc = psutil.Process(pid)
if proc.status() == psutil.STATUS_ZOMBIE:
    # Zombie process!
like image 53
Martijn Pieters Avatar answered Sep 19 '22 08:09

Martijn Pieters