2015年4月12日 星期日

[Linux 常見問題] How to check how long a process has been running?

Source From Here 
Question 
As title. I would like to avoid doing this by launching the process from a monitoring app. 

How-To 
Check man page of command ps
AIX FORMAT DESCRIPTORS
This ps supports AIX format descriptors, which work somewhat like the formatting codes of printf(1) and printf(3). For example, the normal default output can be produced with this: ps -eo "%p %y %x %c". The NORMAL codes are described in the next section.

For example, you can know how long you have login using ssh: 
$ tty
/dev/pts/1
$ ps aux | grep sshd
root 2624 0.0 0.1 66688 1216 ? Ss 17:30 0:00 /usr/sbin/sshd
root 6050 0.0 0.4 100440 4740 ? Ss 21:44 0:00 sshd: root@pts/1
root 6160 0.0 0.0 103252 840 pts/1 S+ 21:52 0:00 grep sshd

$ ps -p 6050 -o etime
ELAPSED
07:42

The ps program gets this from /proc/$$/stat, where one of the fields (see man proc) is process start time. This is, unfortunately, specified to be the time in jiffies (an arbitrary time counter used in the Linux kernel) since the system boot. So you have to determine the time at which the system booted (from /proc/stat), the number of jiffies per second on this system (which turns out to be hard to determine, although some safe guesses can be made as long as you're not worried about keeping it working in the future), and then do the math to get the elapsed time in a useful format. 

So it's convenient that ps does that all for you. :)

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...