Monitoring the process execution and memory consumption in its lifetime

Monitoring the process execution and memory consumption in its lifetime

Monitoring the process execution and memory consumption in its lifetime

Recently, I am working on a research project which involves the monitoring of some processes, I want to get the output of the top command for further analysis, the shell script to do this is as follows:

 1: #!/bin/bash
 2: 
 3: mem_load_file=/tmp/mem_load
 4: for exe in `find ./bin -type f`
 5: do
 6: 	echo $exe >> $mem_load_file
 7: 	./$exe &
 8: 	bgpid=$!
 9: 	echo $bgpid >> $mem_load_file
10: 	top | grep $bgpid >> $mem_load_file &
11: 	wait $bgpid
12: 	killall top
13: done

The mechanism behind this script is to start the process as a background process (line 7) and get its pid (line 8), the pid is used for top and grep, and it is important to make the top command background so that the monitoring can go all the way till the target process exit. The wait call at line 11 makes sure that the process terminate. Then the top can be killed.

The output looks like:

 1: ./bin/sp.W
 2: 861
 3:   861 wujing    20   0   23656   9528    840 R 98.16 0.118   0:00.15 sp.W       
 4:     1 root      20   0   48616   1696    476 S 0.000 0.021   0:00.62 systemd    
 5:   861 wujing    20   0   23656   9528    840 R 99.43 0.118   0:03.14 sp.W       
 6:     1 root      20   0   48616   1696    476 S 0.000 0.021   0:00.62 systemd    
 7: ./bin/cg.W
 8: 865
 9:   865 wujing    20   0   22716   8540    808 R 84.60 0.106   0:00.13 cg.W       
10: ./bin/is.B
11: 869
12:   869 wujing    20   0  274640   8512    256 R 98.21 0.105   0:00.15 is.B       
13:   869 wujing    20   0  274640 120956    412 R 99.48 1.497   0:03.14 is.B       
14: ./bin/lu.C
15: 873
16:   873 wujing    20   0  608760 139080    776 R 91.72 1.722   0:00.14 lu.C       
17:   873 wujing    20   0  608760 592104    788 R 99.15 7.329   0:03.12 lu.C       
18:   873 wujing    20   0  608760 593160    812 R 99.47 7.342   0:06.11 lu.C       
19:   873 wujing    20   0  608760 593160    812 R 99.45 7.342   0:09.10 lu.C       
20:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   0:12.08 lu.C       
21:   873 wujing    20   0  608760 593160    812 R 99.46 7.342   0:15.07 lu.C       
22:   873 wujing    20   0  608760 593160    812 R 99.42 7.342   0:18.06 lu.C       
23:   873 wujing    20   0  608760 593160    812 R 99.10 7.342   0:21.04 lu.C       
24:   873 wujing    20   0  608760 593160    812 R 99.45 7.342   0:24.03 lu.C       
25:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   0:27.01 lu.C       
26:   873 wujing    20   0  608760 593160    812 R 99.46 7.342   0:30.00 lu.C       
27:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   0:32.98 lu.C       
28:   873 wujing    20   0  608760 593160    812 R 99.48 7.342   0:35.97 lu.C       
29:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   0:38.95 lu.C       
30:   873 wujing    20   0  608760 593160    812 R 99.45 7.342   0:41.94 lu.C       
31:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   0:44.92 lu.C       
32:   873 wujing    20   0  608760 593160    812 R 99.46 7.342   0:47.91 lu.C       
33:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   0:50.89 lu.C       
34:   873 wujing    20   0  608760 593160    812 R 99.46 7.342   0:53.88 lu.C       
35:   873 wujing    20   0  608760 593160    812 R 99.12 7.342   0:56.86 lu.C       
36:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   0:59.84 lu.C       
37:   873 wujing    20   0  608760 593160    812 R 99.48 7.342   1:02.83 lu.C       
38:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   1:05.81 lu.C       
39:   873 wujing    20   0  608760 593160    812 R 99.47 7.342   1:08.80 lu.C       
40:   873 wujing    20   0  608760 593160    812 R 99.13 7.342   1:11.78 lu.C       
41:   873 wujing    20   0  608760 593160    812 R 99.14 7.342   1:14.76 lu.C       
42:   873 wujing    20   0  608760 593160    812 R 99.48 7.342   1:17.75 lu.C       
43:   873 wujing    20   0  608760 593160    812 R 99.14 7.342   1:20.73 lu.C       
44:   873 wujing    20   0  608760 593160    812 R 99.45 7.342   1:23.72 lu.C       
45:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   1:26.70 lu.C       
46:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   1:29.68 lu.C       
47:   873 wujing    20   0  608760 593160    812 R 99.48 7.342   1:32.67 lu.C       
48:   873 wujing    20   0  608760 593160    812 R 99.15 7.342   1:35.65 lu.C       
49:   873 wujing    20   0  608760 593160    812 R 99.47 7.342   1:38.64 lu.C       
50:   873 wujing    20   0  608760 593160    812 R 99.14 7.342   1:41.62 lu.C

Author: wujing

Created: 2014-09-16 二 10:08

Emacs 24.3.1 (Org mode 8.2.6)

Validate

原文地址:https://www.cnblogs.com/wujingcqu/p/3974304.html