1.CPU占用率高的进程

参数说明: topk 前 k 的进程、samplingTime 采样时间(秒)、采样间隔时间(秒)

  1. #!/bin/bash
  2. TOPK={{topk}}
  3. SECS={{samplingTime}}
  4. INTERVAL={{interval}}
  5. STEPS=$(( $SECS / $INTERVAL ))
  6. TEMP_FILE_PREFIX="/tmp/tat_public_cpu_usage"
  7. echo Watching CPU usage...
  8. for((i=0;i<$STEPS;i++))
  9. do
  10. ps -eocomm,pcpu | tail -n +2 >> $TEMP_FILE_PREFIX.$$
  11. sleep $INTERVAL
  12. done
  13. echo
  14. echo CPU eaters :
  15. cat $TEMP_FILE_PREFIX.$$ | \
  16. awk '
  17. { process[$1]+=$2;}
  18. END{
  19. for(i in process) {
  20. printf("%-20s %s\n",i, process[i]) ;
  21. }
  22. }' | sort -nrk 2 | head -n $TOPK
  23. rm $TEMP_FILE_PREFIX.$$

2.查看目录占用磁盘空间大小

directory 需要查看的目录路径

  1. #!/bin/bash
  2. du -sh {{directory}}

3.显示 linux 内核版本信息

  1. #!/bin/bash
  2. uname -a

4.显示 linux 主机名

  1. #!/bin/bash
  2. hostname

5.显示僵尸进程

  1. #!/bin/bash
  2. processes=$(ps ax -o user,pid,ppid,pgid,args,stat,start,time)
  3. zombies=$(echo -e "${processes}" | grep -E "\s(Z|z|Z.*)\s")
  4. if [ $? == 1 ]; then
  5. echo "no zombie processes exists on machine"
  6. else
  7. echo -e "${processes}" | head -1
  8. echo "$zombies"
  9. fi

6.清理数据

  1. #!/bin/bash
  2. function deletefiles() {
  3. if [ ! -d $2 ]; then
  4. echo "The specified directory("$2") is not exist."
  5. return
  6. fi
  7. expiredTimeUnit=${1: -1}
  8. expiredTimeValue=${1:0:-1}
  9. if [ "$expiredTimeUnit" = "d" ]; then
  10. expiredTime=$(($expiredTimeValue * 24 * 60 * 60))
  11. elif [ "$expiredTimeUnit" = "h" ]; then
  12. expiredTime=$(($expiredTimeValue * 60 * 60))
  13. elif [ "$expiredTimeUnit" = "m" ]; then
  14. expiredTime=$(($expiredTimeValue * 60))
  15. else
  16. echo "The unit("$expiredTimeUnit") of file age is not supported."
  17. return
  18. fi
  19. for file in $(find $2 -type f -name "$3"); do
  20. local currentDate=$(date +%s)
  21. local modifyDate=$(stat -c %Y $file)
  22. local existTime=$(($currentDate - $modifyDate))
  23. if [ $existTime -gt $expiredTime ]; then
  24. echo "File cleaning succeeded,path:"$file"."
  25. rm -f $file
  26. fi
  27. done
  28. }
  29. deletefiles {{delayTime}} {{filePath}} "{{matchPattern}}"

参数说明:
delayTime 文件的有效时间。如 7d(代表7天),1h(代表1小时),5m(代表5分钟),默认是7d
filePath 清理文件路径。如:/root/log/
matchPattern 清理文件匹配格式,如 *.log。 支持正则匹配