天天看点

shell高级技巧-获取进程的运行时长

在进程监控脚本中,我们通常需要根据脚本的参数来确定有哪些性能参数将被收集,当这些性能参数大于最高阈值或小于最低阈值时,监控脚本将根据实际的情况,采取预置的措施,如邮件通知、直接杀死进程等,这里我们给出的例子是收集进程运行时长性能参数。
ps命令的etime值将给出每个进程的运行时长,其格式主要为以下三种:
      1. minutes:seconds,如20:30
      2. hours:minutes:seconds,如1:20:30
      3. days-hours:minute:seconds,如2-18:20:30
      该脚本将会同时处理这三种格式的时间信息,并最终转换为进程所流经的分钟数。
      [[email protected] ~]# cat > test11.sh
      #!/bin/sh
      #1. 通过ps命令获取所有进程的pid、etime和comm数据。
      #2. 再通过grep命令过滤,只获取init进程的数据记录,这里我们可以根据需要替换为自己想要监控的进程名。
      #3. 输出结果通常为:1 09:42:09 init
      pid_string=`ps -eo pid,etime,comm | grep "init" | grep -v grep`
      #3. 从这一条记录信息中抽取出etime数据,即第二列的值09:42:09,并赋值给exec_time变量。
      exec_time=`echo $pid_string | awk '{print $2}'`
      #4. 获取exec_time变量的时间组成部分的数量,这里是3个部分,即时:分:秒,是上述格式中的第二种。
      time_field_count=`echo $exec_time | awk -F: '{print NF}'`
      #5. 从exec_time变量中直接提取分钟数,即倒数第二列的数据(42)。
      count_of_minutes=`echo $exec_time | awk -F: '{print $(NF-1)}'`
            
<span class="token comment">#6. 判断当前exec_time变量存储的时间数据是属于以上哪种格式。</span>
  <span class="token comment">#7. 如果是第一种,那么天数和小时数均为0。</span>
  <span class="token comment">#8. 如果是后两种之一,则需要继续判断到底是第一种还是第二种,如果是第二种,其小时部分将不存在横线(-)分隔符分隔天数和小时数,否则需要将这两个时间字段继续拆分,以获取具体的天数和小时数。对于第二种,天数为0.</span>
  <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$time_field_count</span> -lt 3 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      count_of_hours<span class="token operator">=</span>0
      count_of_days<span class="token operator">=</span>0
  <span class="token keyword">else</span>
      count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $exec_time <span class="token operator">|</span> <span class="token function">awk</span> -F: <span class="token string">'{print <span class="token variable"><span class="token variable">$(</span>NF-2<span class="token variable">)</span></span>}'</span><span class="token variable">`</span></span>
      fields<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print NF}'</span><span class="token variable">`</span></span>
      <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$fields</span> -ne 1 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
          count_of_days<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$1</span>}'</span><span class="token variable">`</span></span>
          count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$2</span>}'</span><span class="token variable">`</span></span>
      <span class="token keyword">else</span>
          count_of_days<span class="token operator">=</span>0
      <span class="token keyword">fi</span>
  <span class="token keyword">fi</span>
  <span class="token comment">#9. 通过之前代码获取的各个字段值,计算出该进程实际所流经的分钟数。</span>
  <span class="token comment">#10. bc命令是计算器命令,可以将echo输出的数学表达式计算为最终的数字值。</span>
  elapsed_minutes<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$count_of_days*1440</span>+<span class="token variable">$count_of_hours*60</span>+<span class="token variable">$count_of_minutes</span>"</span> <span class="token operator">|</span> <span class="token function">bc</span><span class="token variable">`</span></span>
  <span class="token keyword">echo</span> <span class="token string">"The elapsed minutes of init process is"</span> <span class="token variable">$elapsed_minutes</span> <span class="token string">"minutes."</span>
  CTRL+D
  <span class="token punctuation">[</span>[email protected] ~<span class="token punctuation">]</span><span class="token comment"># ./test11.sh</span>
  The elapsed minutes of init process is 577 minutes.
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
</div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" target="_blank" rel="external nofollow"  rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" ><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup" target="_blank" rel="external nofollow" ></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G" target="_blank" rel="external nofollow" ></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang" target="_blank" rel="external nofollow" ></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" rel="external nofollow"  target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
           
在进程监控脚本中,我们通常需要根据脚本的参数来确定有哪些性能参数将被收集,当这些性能参数大于最高阈值或小于最低阈值时,监控脚本将根据实际的情况,采取预置的措施,如邮件通知、直接杀死进程等,这里我们给出的例子是收集进程运行时长性能参数。
ps命令的etime值将给出每个进程的运行时长,其格式主要为以下三种:
      1. minutes:seconds,如20:30
      2. hours:minutes:seconds,如1:20:30
      3. days-hours:minute:seconds,如2-18:20:30
      该脚本将会同时处理这三种格式的时间信息,并最终转换为进程所流经的分钟数。
      [[email protected] ~]# cat > test11.sh
      #!/bin/sh
      #1. 通过ps命令获取所有进程的pid、etime和comm数据。
      #2. 再通过grep命令过滤,只获取init进程的数据记录,这里我们可以根据需要替换为自己想要监控的进程名。
      #3. 输出结果通常为:1 09:42:09 init
      pid_string=`ps -eo pid,etime,comm | grep "init" | grep -v grep`
      #3. 从这一条记录信息中抽取出etime数据,即第二列的值09:42:09,并赋值给exec_time变量。
      exec_time=`echo $pid_string | awk '{print $2}'`
      #4. 获取exec_time变量的时间组成部分的数量,这里是3个部分,即时:分:秒,是上述格式中的第二种。
      time_field_count=`echo $exec_time | awk -F: '{print NF}'`
      #5. 从exec_time变量中直接提取分钟数,即倒数第二列的数据(42)。
      count_of_minutes=`echo $exec_time | awk -F: '{print $(NF-1)}'`
            
<span class="token comment">#6. 判断当前exec_time变量存储的时间数据是属于以上哪种格式。</span>
  <span class="token comment">#7. 如果是第一种,那么天数和小时数均为0。</span>
  <span class="token comment">#8. 如果是后两种之一,则需要继续判断到底是第一种还是第二种,如果是第二种,其小时部分将不存在横线(-)分隔符分隔天数和小时数,否则需要将这两个时间字段继续拆分,以获取具体的天数和小时数。对于第二种,天数为0.</span>
  <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$time_field_count</span> -lt 3 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
      count_of_hours<span class="token operator">=</span>0
      count_of_days<span class="token operator">=</span>0
  <span class="token keyword">else</span>
      count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $exec_time <span class="token operator">|</span> <span class="token function">awk</span> -F: <span class="token string">'{print <span class="token variable"><span class="token variable">$(</span>NF-2<span class="token variable">)</span></span>}'</span><span class="token variable">`</span></span>
      fields<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print NF}'</span><span class="token variable">`</span></span>
      <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$fields</span> -ne 1 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
          count_of_days<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$1</span>}'</span><span class="token variable">`</span></span>
          count_of_hours<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> $count_of_hours <span class="token operator">|</span> <span class="token function">awk</span> -F- <span class="token string">'{print <span class="token variable">$2</span>}'</span><span class="token variable">`</span></span>
      <span class="token keyword">else</span>
          count_of_days<span class="token operator">=</span>0
      <span class="token keyword">fi</span>
  <span class="token keyword">fi</span>
  <span class="token comment">#9. 通过之前代码获取的各个字段值,计算出该进程实际所流经的分钟数。</span>
  <span class="token comment">#10. bc命令是计算器命令,可以将echo输出的数学表达式计算为最终的数字值。</span>
  elapsed_minutes<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$count_of_days*1440</span>+<span class="token variable">$count_of_hours*60</span>+<span class="token variable">$count_of_minutes</span>"</span> <span class="token operator">|</span> <span class="token function">bc</span><span class="token variable">`</span></span>
  <span class="token keyword">echo</span> <span class="token string">"The elapsed minutes of init process is"</span> <span class="token variable">$elapsed_minutes</span> <span class="token string">"minutes."</span>
  CTRL+D
  <span class="token punctuation">[</span>[email protected] ~<span class="token punctuation">]</span><span class="token comment"># ./test11.sh</span>
  The elapsed minutes of init process is 577 minutes.