Python脚本监控过程和子过程 [英] Python script to monitor process and sub-processes

查看:180
本文介绍了Python脚本监控过程和子过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有监控的过程的Python脚本和它的子进程的CPU和内存利用率的过程。

脚本contentiously检查是否该进程或它的一个子进程处于活动状态。一旦过程和它所有的子流程都是非活动状态,python脚本退出。

所以问题,我在这里面临的一个具体过程 -

  1. 进程启动
  2. 进程创建子进程-1
  3. 进程创建子进程-2
  4. 进程创建子进程-3(主动最迟时刻)
  5. 终止子进程3的(此时流程及其所有子流程是无效的)
  6. 进程创建子进程-4(活动的最新时刻)

所以,如果我的脚本检查的,如果过程或子过程中的一个活跃在时间点5返回false并终止。

在-短:有一个很短的时间跨度,其中的过程和它所有的子流程都是不活动状态(即一个子进程死亡,一个新的子进程之间的时间仍有待生成)。如果我的脚本在这个时间点返回false检查工作状态。

任何人都可以请给我提供任何解决方案或解决这一问题。

 而(bIsProcessActive ==真):
bIsProcessActive = FALSE
如果((proc.is_running()和(proc.status = psutil.STATUS_ZOMBIE))!):
        bIsProcessActive = TRUE
        cpu_usage = proc.get_cpu_percent(间隔= 2)
        memory_usage = proc.get_memory_info()。RSS
    对于孩子在proc.get_children():
        如果(!child.is_running()和(child.status = psutil.STATUS_ZOMBIE)):
            //添加到父进程的内存和CPU使用率
            如果(bIsProcessActive ==虚假和childutilization [0] ==真):
                bIsProcessActive = TRUE
 

解决方案

由于您的流程可以有存活子女的父母的死,你不能有效地监督单靠父的信息依据的孩子,因为对于孩子的家长ID变为1当父退出 - 如果你没有管理父临终前让孩子ID,你找不到它了。

要解决这个问题的方法之一是使用进程组ID (PGID),而不是:过程和它的所有后代都会有相同的PGID。 只有当后代不改造自己的过程组领导者,因此,假如这是真的向前发展。

考虑到这一点,你可以随时扫描正在运行的进程,并找到孩子,即使父母不再运行。

它可以帮助如果监控脚本本身成为一个进程组组长 - 的PGID限制为只显示其后裔

查看:

  • os.setpgrp()
  • os.getpgrp()
  • PS -eo PID,PPID,PGID

我的软件构建的分析工具,称为BuildIn监控脚本中使用这种技术,你可以在它偷看这里:的 https://buildin.apartsw.com/wrap.html 。很抱歉的广告

I have a python script which monitors a process and it's sub-processes for CPU and memory utilization by the process.

Scripts contentiously checks whether the process or one of it's sub-process is active. Once process and all it's sub-processes are in inactive state, python script exits.

So problem I am facing here for a specific process is -

  1. Process Starts
  2. Process creates sub-process-1
  3. Process Creates sub-process-2
  4. Process Creates sub-process-3 (Active at latest moment)
  5. Terminate sub-process-3 (At this point process and all of its sub-processes are inactive)
  6. Process Creates sub-process-4 (Active at latest moment)

So if my script checks for the if process or one of the sub-process is active at time-point 5 it returns false and terminates.

In-Short: There is a short time span where the process and all it's sub-processes are in inactive state.(i.e. time between a sub-process killed and a new sub-process is yet to be spawned). If my script checks for active status at this point of time it returns false.

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:

  • os.setpgrp()
  • os.getpgrp()
  • ps -eo pid,ppid,pgid

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

这篇关于Python脚本监控过程和子过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆