zoukankan      html  css  js  c++  java
  • 脚本中如何控制子进程的生命周期

    最近写了一个分析日志的脚本,需要取很多不同服务的日志,分别分析后将结果录入数据库显示,顺序执行要消耗大量时间,由于取日志和分析的操作对于每个服务是独立的,因此将其用子进程后台执行。

    执行方法为:

    {
    rsync --bwlimit ${remoteip1}::root${remotedir1}/${remotelog1} ${destlog1}
    sh process.sh ${destlog1}
    } &
    
    {
    rsync --bwlimit ${remoteip2}::root${remotedir2}/${remotelog2} ${destlog2}
    sh process.sh ${destlog2}
    } &
    
    wait
    
    php insert_db.php

    但这样我们必须等待所有的后台进程执行完毕后继续执行主进程,而这里主进程的执行是有时间限制的(5分钟内),如果某个后台进程在处理时出现异常耗时较久,我们必须等待其执行完才能继续执行主进程。因此在执行超时后,需要杀掉超时的子进程,以便脚本能在指定的时间内执行完毕

    增加代码如下:

    
    
    {
    rsync --bwlimit ${remoteip1}::root${remotedir1}/${remotelog1} ${destlog1}
    sh process.sh ${destlog1}
    } &
    
    {
    rsync --bwlimit ${remoteip2}::root${remotedir2}/${remotelog2} ${destlog2}
    sh process.sh ${destlog2}
    } &

    sleep 300

    ps -f --ppid $$ | grep -v "ps -f" | awk -F" " '{if($2!="PID")print $2}' | while read pid
    do
      kill -9 $pid
    done
    wait
    
    php insert_db.php
     

    其中$$是获取当前脚本的进程号,ps -f --ppid $$获得所有以$$为父进程的进程列表,grep -v "ps -f" 为过滤掉ps命令本身,awk将PID列筛选出来,最后通过while循环将所有子进程杀死。

    这样我们就在等待了5分钟后将所有未执行完的子进程kill掉了

  • 相关阅读:
    激活Win Server 2008 R2 Datacenter
    .NET中使用EF6与连接MYSQL
    设计模式(六)——命令模式
    C#爬虫之Senlium
    GitHub入门(一)GIT配置与Hexo博客搭建
    正则表达式
    C#预处理器
    第一次炒花甲
    第一次清蒸鲈鱼
    Python traceback【转】
  • 原文地址:https://www.cnblogs.com/django/p/3159964.html
Copyright © 2011-2022 走看看