【问题标题】:find all users who has over N process and echo them in shell找到所有拥有超过 N 个进程的用户并在 shell 中回显它们
【发布时间】:2013-03-19 04:21:21
【问题描述】:

我写的脚本是ksh。需要找到所有拥有超过 N 个进程的用户并在 shell 中回显它们。 N 从 ksh 读取。

我知道我应该使用什么 ps -elf 但如何解析它,找到具有 >N 进程的用户并用他们创建数组。 ksh 中数组的小麻烦。请帮忙。也许简单的解决方案可以帮助我而不是创建数组。

s162103@helios:/home/s162103$ ps -elf 
     0 S  s153308  4804     1   0  40 20        ?  17666        ? 11:03:08 ?           0:00 /usr/lib/gnome-settings-daemon --oa
     0 S     root  6546  1327   0  40 20        ?   3584        ? 11:14:06 ?           0:00 /usr/dt/bin/dtlogin -daemon -udpPor
     0 S webservd 15646   485   0  40 20        ?   2823        ?     п╪п╟я─я ?           0:23 /opt/csw/sbin/nginx
     0 S  s153246  6746  6741   0  40 20        ?  18103        ? 11:14:21 ?           0:00 iiim-panel --disable-crash-dialog
     0 S  s153246 23512     1   0  40 20        ?  17903        ? 09:34:08 ?           0:00 /usr/bin/metacity --sm-client-id=de
     0 S     root   933   861   0  40 20        ?   5234        ? 10:26:59 ?           0:00 dtgreet -display :14
     ...

当我输入时

ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=1

s162103@helios:/home/s162103$ ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=1
root 118
/usr/sadm/lib/smc/bin/smcboot 3
/usr/lib/autofs/automountd 2
/opt/SUNWut/lib/utsessiond 2
nasty 31
dima 22
/opt/oracle/product/Oracle_WT1/ohs/ 7
/usr/lib/ssh/sshd 5
/usr/bin/bash 11

那不是用户 /usr/sadm/lib/smc/bin/smcboot ps -elf 中有最后一个字段,而不是用户

【问题讨论】:

    标签: linux shell unix ksh


    【解决方案1】:

    类似这样的东西(假设你的 ps 命令的第三个字段给出了用户 ID):

     ps -elf | 
       awk '{a[$3]++;}
       END {
         for(i in a)
           if (a[i]>N)
             print i, a[i];
       }' N=3
    

    【讨论】:

    • 是的第三个字段,但它输出错误的信息。它输出随机信息(UID 和 CMD 以及来自 ps -elf 的其他信息),不仅是我只需要名称的名称。(UIDs)
    • F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 5 S cl32387 18246 18241 0 80 0 - 5895 ? Mar16 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vs5 S cl32387 18469 18436 0 80 0 - 13200 ? 09:51 ? 00:00:00 sshd: cl32387@pts/2 0 S cl32387 18481 18469 0 80 0 - 4470 - 09:51 pts/2 00:00:00 -bash 0 R cl32387 18842 18481 0 80 0 - 3431 - 09:51 pts/2 00:00:00 ps -elf
    • 我得到了相同的输出。请记住,提供的解决方案将列出所有用户及其编号。如果超过 3 个(N=3),则进程数。
    • 顶部是我编辑的帖子。大师,请检查一下。有关于输出 ps -elf ant your command in formatted 的信息
    • 使用 nawk 可以正常工作,但计数错误。例如:root 136ps -elf 显示小于 136
    【解决方案2】:

    您要在此处使用的最小ps 命令是ps -eo user=。这只会打印每个进程的用户名,仅此而已。剩下的可以用 awk 来完成:

    ps -eo user= |
      awk  -v max=3 '{ n[$1]++ }
        END {
          for (user in n)
            if (n[user]>max)
              print n[user], user
      }'
    

    为了便于阅读,我建议将计数放在第一列。

    【讨论】:

      【解决方案3】:
      read number    
      ps -elfo user= | sort | uniq -c | while read count user
      do 
          if (( $count > $number )) 
          then 
            echo $user  
          fi  
      done
      

      这是最好的解决方案,而且有效!

      【讨论】:

        猜你喜欢
        • 2021-03-26
        • 1970-01-01
        • 2022-11-27
        • 2012-10-17
        • 1970-01-01
        • 2018-04-17
        • 2021-06-14
        • 2021-11-04
        • 1970-01-01
        相关资源
        最近更新 更多