Chinaunix首页 | 论坛 | 博客
  • 博客访问: 35195
  • 博文数量: 8
  • 博客积分: 152
  • 博客等级: 入伍新兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-23 15:02
文章分类
文章存档

2015年(1)

2011年(7)

分类: Python/Ruby

2011-08-16 14:59:01

 
  1. $ ps aux
  2.       PID PPID PGID WINPID TTY UID STIME COMMAND
  3.      1512 1 1512 3644 con 1003 09:27:17 /usr/bin/bash
  4. I    3252 1512 3252 3232 con 1003 09:27:19 /usr/bin/bash
  5.      4640 1 4640 4640 con 1003 13:12:52 /usr/bin/bash
  6.      5592 4640 5592 6120 con 1003 15:01:34 /usr/bin/ps

ps命令的格式如上,可根据本机环境调整下面的脚本:
    1. #!/bin/bash
    2. root=$1
    3. root=${root:=1}

    4. ps aux | awk -F'[ ]+' -vroot=$root '
    5. function pad(c, n) {
    6.     r = ""
    7.     for(j=0;j
    8.         if (j < n)
    9.             r = r " "
    10.         else {
    11.             if (j==n) {
    12.                 r = r "|\n" r "|"
    13.             }
    14.             r = r c
    15.         
    16.         }
    17.     }
    18.     return r
    19. }
    20. function findChild(ppid) {
    21.         depth++
    22.         for (i in p) {
    23.             if (p[i]==ppid) {
    24.                 print pad("-", depth) i"--"pc[i]
    25.                 p[i]=""
    26.                 findChild(i)
    27.             }
    28.         }
    29.         depth--
    30.         
    31. }
    32. NR>1{p[$2]=$3;pc[$2]=$NF}
    33. END{
    34.     depth = 1
    35.      #root = 1
    36.     if (root in p) print root"--"pc[root]
    37.     p[root]=""
    38.     findChild(root)

    39. }'
    40. 效果截图:
阅读(1836) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~