常用的shell脚本命令(将持续更新中)

  • 查看CPU占用率高的进程
#!/bin/bash
TOPK={{topk}}
SECS={{samplingTime}}
INTERVAL={{interval}}
STEPS=$(( $SECS / $INTERVAL ))
TEMP_FILE_PREFIX="/tmp/tat_public_cpu_usage"

echo Watching CPU usage...
for((i=0;i<$STEPS;i++))
do
  ps -eocomm,pcpu | tail -n +2 >> $TEMP_FILE_PREFIX.$$
  sleep $INTERVAL
done

echo
echo CPU eaters :
cat $TEMP_FILE_PREFIX.$$ | \
awk '
{ process[$1]+=$2;}

END{
  for(i in process) {
    printf("%-20s %s\n",i, process[i]) ;
  }
}' | sort -nrk 2 | head -n $TOPK

rm $TEMP_FILE_PREFIX.$$
 
参数名
参数值
描述
topk
 
前k个进程
samplingTime
 
采样时间, 单位秒
interval
 
采样间隔时间, 单位秒

 

  • 查看目录占用磁盘空间大小
#!/bin/bash
du -sh {{directory}}
 
参数名
参数值
描述
directory
 
目标目录

 

  • 查看TAT agent版本信息
#!/bin/bash
/usr/local/qcloud/tat_agent/tat_agent --version

 

  • 显示 linux 内核版本信息
#!/bin/bash
uname -a

 

  • 显示主机名
#!/bin/bash
hostname

 

  • 显示僵尸进程
#!/bin/bash
processes=$(ps ax -o user,pid,ppid,pgid,args,stat,start,time)
zombies=$(echo -e "${processes}" | grep -E "\s(Z|z|Z.*)\s")
if [ $? == 1 ]; then
  echo "no zombie processes exists on machine"
else
  echo -e "${processes}" | head -1
  echo "$zombies"
fi

 

  • 批量在Linux实例安装或卸载yum/apt包

#!/bin/bash
function configurePackages() {
    installer=$1
    action=$2
    packageName=$3
    if [ "$installer" = "yum" ]; then
        if [ "$action" = "install" ]; then
            yum install -y $packageName
            if [ $? -ne 0 ]; then
                echo "Package install failed. Please check your command"
                exit 1
            fi
        elif [ "$action" = "uninstall" ]; then
            yum remove -y $packageName
            if [ $? -ne 0 ]; then
                echo "Package uninstall failed. Please check your command"
                exit 1
            fi
        else
            echo "Package command must be install or uninstall"
            exit 1
        fi
    elif [ "$installer" = "apt-get" ]; then
        if [ "$action" = "install" ]; then
            apt-get -y install $packageName
            if [ $? -ne 0 ]; then
                echo "Package install failed. Please check your command"
                exit 1
            fi
        elif [ "$action" = "uninstall" ]; then
            apt-get -y remove $packageName
            if [ $? -ne 0 ]; then
                echo "Package uninstall failed. Please check your command"
                exit 1
            fi
        else
            echo "Package command must be install or uninstall"
            exit 1
        fi
    else
        echo "Unknown package installer. Only support yum/apt-get"
        exit 1
    fi
}
  
configurePackages {{installer}} {{action}} {{packageName}}
 
参数名
参数值
描述
installer
apt-get
包管理器
action
install
安装或卸载 package
packageName
 
yum/apt-get安装的包名

 

  • 批量在Linux实例清理磁盘
#!/bin/bash
  
function deletefiles() {
  if [ ! -d $2 ]; then
    echo "The specified directory("$2") is not exist."
    return
  fi
  
  expiredTimeUnit=${1: -1}
  expiredTimeValue=${1:0:-1}
  
  if [ "$expiredTimeUnit" = "d" ]; then
    expiredTime=$(($expiredTimeValue * 24 * 60 * 60))
  elif [ "$expiredTimeUnit" = "h" ]; then
    expiredTime=$(($expiredTimeValue * 60 * 60))
  elif [ "$expiredTimeUnit" = "m" ]; then
    expiredTime=$(($expiredTimeValue * 60))
  else
    echo "The unit("$expiredTimeUnit") of file age is not supported."
    return
  fi
  
  for file in $(find $2 -type f -name "$3"); do
    local currentDate=$(date +%s)
    local modifyDate=$(stat -c %Y $file)
    local existTime=$(($currentDate - $modifyDate))
  
    if [ $existTime -gt $expiredTime ]; then
      echo "File cleaning succeeded,path:"$file"."
      rm -f $file
    fi
  done
}
  
deletefiles {{delayTime}} {{filePath}} "{{matchPattern}}"

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

给TA买糖
共{{data.count}}人
人已赞赏
技术专区

PHP版AES对称加密(复制可用)

2021-10-19 18:55:27

技术专区

redis 集群配置

2021-11-20 10:28:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索