I have a Python script which monitors a process and its sub-processes for CPU and memory utilization by the process.
The script continuously checks whether the process or one of its sub-processes is Active
. Once the process and all its sub-processes are in an Inactive
state, the Python script exits.
So, the problem I am facing here for a specific process is -
- Process starts
- Process creates sub-process-1
- Process creates sub-process-2
- Process creates sub-process-3 (
Active
at latest moment) - Terminate sub-process-3 (At this point the process and all of its sub-processes are
Inactive
) - Process creates sub-process-4 (
Active
at latest moment)
So, if my script checks whether the process or one of the sub-processes is Active
at [5] it returns false and terminates!
In short: There is a short time span where the process and all its sub-processes are in an Inactive
state (i.e. the time between a sub-process killed and a new sub-process is yet to be spawned). If my script checks for the status at this point of time, it returns false, because everything is inactive.
Could anyone please provide me with any solution or address this problem?
while(bIsProcessActive == True):
bIsProcessActive = False
if (proc.is_running() and proc.status != psutil.STATUS_ZOMBIE):
bIsProcessActive = True
cpu_usage = proc.get_cpu_percent(interval = 2)
memory_usage = proc.get_memory_info().rss
for child in proc.get_children():
if child.is_running() and child.status != psutil.STATUS_ZOMBIE:
// Add to memory and CPU usage of parent process
if bIsProcessActive == False and childutilization[0] == True:
bIsProcessActive = True
Since your process can have children surviving the parent's death you can't effectively monitor the children based solely on the parent information since the parent ID for the child becomes 1 when the parent exits - if you didn't manage to get the child ID before the parent death you can't locate it anymore.
One way to address this is to use the process group ID (pgid) instead: the process and all its descendents will have the same pgid. Only works if the descendents don't transform themselves in process group leaders, so assuming this to be true going forward.
With this in mind you can always scan the running processes and locate the children even if the parent is no longer running.
It may help if the monitoring script itself becomes a process group leader - restricts the pgid to only its descendants.
Look at:
I used this technique in a monitoring script for a sw build analysis tool called BuildIn, you can peek at it here: https://buildin.apartsw.com/wrap.html. Sorry for the advertisement