统计/proc目类下Linux进程相关数量信息,输出总进程数,running进程数,stoped进程数,sleeing进程数,zombie进程数-《shell脚本》

admin 2025-11-06 14:31:32 系统网络 来源:ZONE.CI 全球网 0 阅读模式

    输出所有 zombie 的进程到 zombie.txt 杀死所有 zombie 进程。

    1. #!/bin/bash
    2. ALL_PROCESS=$(ls /proc/ | egrep '[0-9]+')
    3. running_count=0
    4. stoped_count=0
    5. sleeping_count=0
    6. zombie_count=0
    7. for pid in ${ALL_PROCESS[*]}
    8. do
    9. test -f /proc/$pid/status && state=$(egrep "State" /proc/$pid/status | awk
    10. '{print $2}')
    11. case "$state" in
    12. R)
    13. running_count=$((running_count+1))
    14. ;;
    15. T)
    16. stoped_count=$((stoped_count+1))
    17. ;;
    18. S)
    19. sleeping_count=$((sleeping_count+1))
    20. ;;
    21. Z)
    22. zombie_count=$((zombie_count+1))
    23. echo "$pid" >>zombie.txt
    24. kill -9 "$pid"
    25. ;;
    26. esac
    27. done
    28. echo -e "total:
    29. $((running_count+stoped_count+sleeping_count+zombie_count))\nrunning:
    30. $running_count\nstoped: $stoped_count\nsleeping: $sleeping_count\nzombie:
    31. $zombie_count"
    01-shell脚本介绍-《shell脚本》 系统网络

    01-shell脚本介绍-《shell脚本》

    一、shell脚本是什么二、为什么要学shell,而不是其他计算机语言三、学习这门课程的优势四、学了能干什么五、学习什么内容六、学习的技巧七、成长路径八、学习环
    评论:0   参与:  13