日期:2014-05-16  浏览次数:20616 次

如何友好的 监控与管理 Linux 进程
在做视频编解码的程序,需要fork子进程去调用转码的tools做encode, 这里面就需要能很好的控制进程,让进程类似于跑在沙漏里面,安全,可控的运行,

实现进程的管理接口,其实比较简单的,所有的进程信息来源,均来自 /proc 文件系统

因为是所有进程相关的信息都是通过读取内存proc文件系统,所以没有任何性能问题,

tommy@tommy-desktop:~$ cd /proc/
tommy@tommy-desktop:/proc$ ls
1     1176   13    1340  14    1451   15736  20444  20460  240  33    43  59   636  756  8     9299       crypto       irq            modules       swaps
10    11887  1300  1346  1402  1461   16     20445  20461  25   34    44  594  640  76   80    9305       devices      kallsyms       mounts        sys
1025  12     1306  1347  1404  1462   1675   20446  20462  257  35    45  596  641  760  81    9319       diskstats    kcore          mtrr          sysrq-trigger
1085  1214   1309  1352  1413  1467   17     20447  20646  26   36    46  598  66   768  82    9321       dma          key-users      net           sysvipc
1091  1232   1310  1358  1414  14734  18     20448  21     27   362   5   6    67   77   828   9323       dri          kmsg           pagetypeinfo  timer_list
1098  1263   1313  1363  1428  1496   19     20449  2164   28   37    51  60   7    770  83    9340       driver       kpagecount     partitions    timer_stats
11    1271   1314  1365  1430  15     2      20450  218    284  38    52  61   70   773  833   acpi       execdomains  kpageflags     sched_debug   tty
1105  1273   1318  1367  1432  151    20     20451  22     287  39    53  618  71   779  84    asound     fb           latency_stats  schedstat     uptime
1134  1277   1325  1370  1433  1517   20439  20453  23     29   4     54  62   716  78   9     buddyinfo  filesystems  loadavg        scsi          version
1135  1285   1327  1376  1436  1520   20440  20454  236    3    40    55  621  72   781  9043  bus        fs           locks          self          version_signature
1136  1287   1333  1381  1441  15355  20441  20457  237    30   4093  56  627  73   784  9045  cgroups    interrupts   mdstat         slabinfo      vmallocinfo
1161  1288   1334  1386  1443  15550  20442  20458  238    31   41    57  630  74   785  9047  cmdline    iomem        meminfo        softirqs      vmstat
1170  1294   1339  1391  1449  15596  20443  20459  239    32   42    58  632  75   79   922   cpuinfo    ioports      misc           stat          zoneinfo
tommy@tommy-desktop:/proc$ 


所以对进程的信息采集,都是可以通过读取/proc文件系统获得

但已经有开源实现如下

A process utilities module

http://code.google.com/p/psutil/


获得当前系统上所有正在运行的进程(pid)

tommy@tommy-desktop:~/workerpool/worker/apps/health$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.get_pid_list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 66, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 151, 218, 236, 237, 238, 239, 240, 257, 284, 287, 362, 422, 425, 594, 596, 598, 618, 621, 627, 630, 632, 636, 640, 641, 716, 756, 760, 768, 770, 773, 779, 781, 784, 785, 828, 833, 922, 1025, 1085, 1091, 1098, 1105, 1134, 1135, 1136, 1161, 1170, 1176, 1214, 1232, 1263, 1271, 1273, 1277, 1285, 1287, 1288, 1294, 1300, 1306, 1309, 1310, 1313, 1314, 1318, 1325, 1327, 1333, 1334, 1339, 1340, 1346, 1347, 1352, 1358, 1363, 1365, 1367, 1370, 1376, 1381, 1386, 1391, 1402, 1404, 1413, 1414, 1428, 1430, 1432, 1433, 1436, 1441, 1443, 1449, 1451, 1461, 1462, 1467, 1496, 1517, 1520, 1675, 2164, 4093, 9043, 9045, 9047, 9299, 9305, 9319, 9321, 9323, 9340, 11887, 13354, 14734, 15355, 15550, 15596, 15620, 15621, 15629, 15630, 15735]
>>> 


获得指定进程pid的一些详尽信息以及控制

主要信息是:
1. get_cpu_percent(