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掉了

  • 相关阅读:
    框架Frameset 的JS 跳转 刷新 [转]
    Javascript的变量与delete操作符
    字符串替换方法的优劣
    Nutch介绍(译)
    权限系统—数据授权模型
    Apache Synapse介绍(译)
    java动态代理
    ASM简介
    maven使用技巧——构件搜索
    springSecurity源码分析——org.springframework.security.web.FilterInvocation类
  • 原文地址:https://www.cnblogs.com/django/p/3159964.html
Copyright © 2011-2022 走看看