最新消息:

如何判定linux的进程运行时长和何时启动

shell 大步 1180浏览 0评论

Ps:额,这是我自己翻译的一篇文章,翻译不好勿喷,谢谢!
##################################以下为英语四级压线的我的翻译
文章的大概意思就是说这个作者谢了写了个监视用的脚本用来判断linux的进程工作了多长时间以及进程何时启动的,工作原理大致是通过 /proc/<PID>/stat file来得到所要的信息,其中主要通过jiffies这个全局变量来得到时间,
因为jiffies是用来记录自系统启动以来产生的节拍总数,。启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值。一秒内时钟中断的次数等于Hz,所以jiffies一秒内增加的值也就是Hz。系统运行时间以秒为单位,等于jiffies/Hz。 作者系统上的频率是100Hz.
    Ok,余下的我想大都看的懂了吧,不懂谷歌去。说句题外话,还不知道上次的六级考试成绩怎么样,虽然对了网上的答案,正确率还可以,但是鬼知道那些改卷子的老头是怎么算分的,权重这东西伤不起呀。
   
#############################################
原文链接:http://www.frederikkonietzny.de/2012/03/how-to-determine-the-runtime-and-start-time-of-a-linux-process/

Yesterday, I needed to determine the runtime of a Linux process for a monitoring script.
Cos the format for start_time of the ps command may change if the process was not started in the same year, I decided to take the neccessary informations from the /proc/<PID>/stat file.
In this file the process start time since boot is defined at the twenty-second field, expressed in Jiffies – the scale unit of the system timer. (One Jiffie is one tick of the system timer).
To convert Jiffies to seconds I just have to divide the number of Jiffies by the frequency (hertz) of the system timer, which is defined in the Linux Kernel header file include/asm-generic/param.h. The frequency may differ between Linux kernel versions and hardware platform! On my Linux systems the frequency is 100 HZ.

In a shell script the following line will determine the start-time of a process since boot time.

To get the absolute start time I am adding the boot time of the Linux system from the /proc/stat file to the start time of
the process.

To get the runtime in seconds of the process I just subtract the absolute start time of the process from the actual time.

Download the nagios / icinga plugin from my projects page or the check the following example which shows a simple shell script that takes a process string and determine the runtime and the start time.
  

Output of the script for the pidgin process.


转载请注明:大步's Blog » 如何判定linux的进程运行时长和何时启动

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
SiteMap