那些瞎写的shell脚本(不断更新)

check_cpu-mem.sh

#!/bin/bash


cpu_num=$(top -n 1|awk -F ' ' '{print $10}'|awk 'NR==8'|awk -F '.' '{print $1}')
#mem_num=$(top -n 1|awk -F ' ' '{print $11}'|awk 'NR==8')

detail=$(top -n 1)

date=$(date +%Y-%m-%d\ %H:%M:%S)

#echo $cpu_num >> /soft/scripts/top_detail.txt
if [ $cpu_num -ge 80 ];then
        echo $date >> /soft/scripts/top_detail.txt
        echo $detail >> /soft/scripts/top_detail.txt
        echo "========================================" >> /soft/scripts/top_detail.txt
        echo "========================================" >> /soft/scripts/top_detail.txt
        echo "================这是分界线========================" >> /soft/scripts/top_detail.txt
        echo "========================================" >> /soft/scripts/top_detail.txt
        echo "========================================" >> /soft/scripts/top_detail.txt
fi


for i in {1..10}
do
mem_num=$(top -n 1|awk -F ' ' '{print $11}'|awk 'NR==8')
echo $mem_num |grep :
if [ $? -eq 0 ];then
        sleep 2
        echo '无效数据'
else
        mem2_num=$(echo $mem_num|awk -F '.' '{print $1}')
        echo $mem2_num >> /soft/scripts/top_detail.txt
        if [ $mem2_num -ge 80 ];then
                echo $date >> /soft/scripts/top_detail.txt
                echo $detail >> /soft/scripts/top_detail.txt
                echo "========================================" >> /soft/scripts/top_detail.txt
                echo "========================================" >> /soft/scripts/top_detail.txt
                echo "================这是分界线========================" >> /soft/scripts/top_detail.txt
                echo "========================================" >> /soft/scripts/top_detail.txt
                echo "========================================" >> /soft/scripts/top_detail.txt
        fi

fi
done

check_php-fpm.sh

#!/bin/bash

php_num=$(ps -ef|grep php-fpm|wc -l)

if [ $php_num -ge 200 ];then
        echo "web128 php进程剧增" | mail -s 'The Watch' 810540895@qq.com
fi

check_tcp.sh

#!/bin/bash


redis=$(netstat -n |grep TIME_WAIT|grep 6379|wc -l)
mysql=$(netstat -n |grep TIME_WAIT|grep 3306|wc -l)
max=$(netstat -n |awk -F ' ' '{print $5}'|sort -r |uniq -c|sort -n|awk -F ' ' '{print $1}' |tail -n 1)
max_detail=$(netstat -n |awk -F ' ' '{print $5}'|sort -r |uniq -c|sort -n|awk -F ' ' '{print $1"\t"$2}' |tail -n 5)
date=$(date +%Y-%m-%d\ %H:%M:%S)

totle=$(netstat -an|wc -l)

ESTABLISHED=$(netstat -anp|grep ESTABLISHED|wc -l)
ESTABLISHED_detail=$(netstat -anp|grep ESTABLISHED)

if [ $redis -ge 10000 ];then
        echo "redis数量: " $redis "时间:" $date >> /soft/scripts/check_tcp.txt
fi

if [ $mysql -ge 10000 ];then
        echo "mysql数量: " $redis "时间:" $date >> /soft/scripts/check_tcp.txt
fi


if [ "$max" -ge 10000 ];then
        echo "最大可疑进程数量:" $max "可疑进程:" $max_detail "时间:" $date  >> /soft/scripts/detail.txt
fi

if [ $totle -ge 10000 ];then
        echo $date >> /soft/scripts/totle.txt
        echo $(netstat -an) >> /soft/scripts/totle.txt
        echo "========================================" >> /soft/scripts/totle.txt
        echo "========================================" >> /soft/scripts/totle.txt
        echo "================这是分界线========================" >> /soft/scripts/totle.txt
        echo "========================================" >> /soft/scripts/totle.txt
        echo "========================================" >> /soft/scripts/totle.txt
fi


if [ $ESTABLISHED -ge 300 ];then
        echo $date >> /soft/scripts/ESTABLISHED_detail.txt
        echo $ESTABLISHED_detail >> /soft/scripts/ESTABLISHED_detail.txt
        echo "========================================" >> /soft/scripts/ESTABLISHED_detail.txt
        echo "========================================" >> /soft/scripts/ESTABLISHED_detail.txt
        echo "================这是分界线========================" >> /soft/scripts/ESTABLISHED_detail.txt
        echo "========================================" >> /soft/scripts/ESTABLISHED_detail.txt
        echo "========================================" >> /soft/scripts/ESTABLISHED_detail.txt
fi

nginx_log_mv.sh

#!/bin/bash
mv /txdata/server/openresty-1.13.6.2/nginx/logs/access.log /txdata/server/openresty-1.13.6.2/nginx/logs/access.log.$(date +%F)
if [ $? -eq 0 ];then
        nginx -s reload
fi

sed.sh(替换某个文件夹下面的所有文件中的字符串为另一个字符串)

#!/bin/bash
for file in /biz-code/crontab/check/*
do
sed -i 's/dev/check/g' $file
done

restart_project.sh

#!/bin/bash

#root_path="/biz-code"
root_path='/biz-code'
git_path="https://gitee.com/xxxxx"
init_file=("init.sh.dev" "init.sh.check" "init.sh.release")
projects=("aristotle" "hume" "plato" "socrates" "marx");


#项目下载
read -p "请输入项目编号: 【'1:aristotle' '2:hume' '3:plato' '4:socrates' '5:marx'】 " project
cd $root_path
git_full_path=$git_path/${projects[$project-1]}.git
echo ""
echo "========================"
echo "当前路径:" $root_path
echo "需要下载的项目:" ${projects[$project-1]}
echo "git地址:" $git_full_path 
echo ""

#判断路径是否存在(存在不git clone)
if [ ! -d ${projects[$project-1]} ];then
   
   #二次确认是否下载
   read -r -p "是否进行下载? [Y/n] " input
   if  [ $input != 'Y' ] &&[ $input != 'y' ]
   then
        echo "取消下载"
        exit 0
   fi

   #git clone 项目
   git clone $git_full_path
   if [ $? -eq 0 ];then
      echo '项目代码已下载'
   else
      echo '项目代码下载失败,请检查' $soucre_address
      exit
   fi
else
   echo "========================"   
   echo "项目已经存在"
	
fi



cd $root_path/${projects[$project-1]}
path=`pwd`
echo ""
echo "========================"
echo "当前路径:" $path
echo ""

# 拉取最新分支
git pull
if [ $? -eq 0 ];then
    echo "拉取代码成功"
else
    echo "拉取代码失败"
    exit
fi

#切换分支
read -p "请输入分支: " branch
git checkout $branch
git pull
if [ $? -eq 0 ];then
	echo "分支切换成功"
else
	echo "分支切换失败,请检查"
	exit
fi
echo ""
echo "========================"
echo  "当前分支:" $branch
echo "" 


#切换环境
read -p "请输入环境编号:【1:测试服 2:验收服 3:正式服】: " env
if [[ $env > "3" || $env == "0" ]]
then
     echo "环境数据错误"
     exit 0
fi	


docker_file=''
script_path=''
#workerman
if [[ $project == 1 || $project == 2 ]];then
	script_path='./Scripts'
	docker_file=$script_path/docker-compose.yml
# thinkphp5
elif [[ $project == 3 ]];then
	script_path='./scripts'
	docker_file=$script_path/plato.compose
elif [[ $project == 4 ]];then
	script_path='./scripts'
	docker_file=$script_path/docker.compose
fi 

cp $script_path/${init_file[$env-1]} init.sh && chmod +x init.sh && ./init.sh
        if [ $? -eq 0 ];then
                echo '环境切换成功'
        else
                echo '环境切换失败,请检查'
                exit
        fi

#go

# 启动项目
echo ""
echo "正在启动项目"
echo $docker_file
echo ""
docker-compose -f $docker_file stop
docker-compose -f $docker_file up -d
if [ $? -eq 0 ];then
	echo '项目重新启动成功'
else
        echo '项目重启失败,请检查'
fi

check_env.sh

#!/bin/bash

project=(redis elastic kibana web php hume aristotle)
for i in ${project[*]}
do
	docker ps |grep $i
done
if [ $? -eq 0 ];then
echo ''
else
	for i in ${project[*]}
	do
        	docker restart $i
	done
fi


merge_branch.sh 定时合并线上分支到各个环境

#!/bin/bash

socrates=$(ssh zhubaoe@172.16.0.40 "cd /biz-code/socrates;git branch" |grep '*' |awk -F ' ' '{print $2}')
aristotle=$(ssh zhubaoe@172.16.0.21 "cd /biz-code/aristotle;git branch" |grep '*' |awk -F ' ' '{print $2}')
hume=$(ssh zhubaoe@172.16.0.21 "cd /biz-code/hume;git branch" |grep '*' |awk -F ' ' '{print $2}')

kobe_branch=$(ssh zhubaoe@172.16.0.9 "docker ps|grep release_v" | awk -F ' ' '{print $2}' | awk -F '_' '{print $3}' |sort | tail -n1)
s1=$( echo $kobe_branch |awk -F '.' '{print $1}')
s2=$( echo $kobe_branch |awk -F '.' '{print $2}')
s3=$( echo $kobe_branch |awk -F '.' '{print $3}')
kobe=$s1.$s2.$s3


ck(){
  if [ $? -eq 0 ];then
	echo ''
  else
	#	echo "分支合并有问题" | mail -s 'branch merge' 810540895@qq.com
	curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fab4f30b-43f5-4a7a-b606-xxxxxxxx'    -H 'Content-Type: application/json'    -d '
   {                      
        "msgtype": "text",
        "text": {                   
            "content": "分支合并有问题!!有时间看看 @所有人"
        }
   }'

	exit
  fi
}

cd /biz-code/socrates
git pull
git checkout $socrates
ck
git pull
git checkout dev
ck
git pull
git merge $socrates --no-edit
ck
git push
git checkout test
ck
git pull
git merge $socrates --no-edit
ck
git push
git checkout check
ck
git pull
git merge $socrates --no-edit
ck
git push
ck

cd /biz-code/aristotle
git pull
git checkout $aristotle
ck
git pull
git checkout dev
ck
git pull
git merge $aristotle --no-edit
ck
git push
git checkout test
ck
git pull
git merge $aristotle --no-edit
ck
git push
git checkout check
ck
git pull
git merge $aristotle --no-edit
ck
git push
ck

cd /biz-code/hume
git pull
git checkout $hume
ck
git pull
git checkout dev
ck
git pull
git merge $hume --no-edit
ck
git push
git checkout test
ck
git pull
git merge $hume --no-edit
ck
git push
git checkout check
ck
git pull
git merge $hume --no-edit
ck
git push
ck

checkBee.sh

#! /bin/sh
echo "check bee start"
bees=("order" "notice" "oauth" "res" "lcc" "rbac" "pay" "redis" "sett" "ict" "profit" "ops" "gosin" "qecgosin" "member" "hjl" "charge" "app" "gateway")
for i in ${bees[@]};
do
   pid=`ps -ef | grep cbest-bee-${i} | grep -v grep | awk '{print $2}'`
   if [ ! $pid  ]; then
      nohup  /usr/local/java/jdk1.8.0_241/bin/java -Xms384m -Xmx384m -jar /root/data/cbest-bee-${i}/cbest-bee-${i}-0.0.1-SNAPSHOT.jar   >/dev/null 2>&1 &
   fi
   sleep 30
done

deploy.sh

#!/bin/bash

help(){
 echo -e " USAGE EXAMPLE: sh delpoy.sh -n eureka-prd   \n -h help. \n -p project name :eureka-prd \n -v project version :1.0-RELEASE  "
 exit 0
}
while [ -n "$1" ]; do
        case $1 in
                -h) help;shift 1;;# shift 清除 -h
                -p) project=$2;shift 2;;#shift 清除 -n 项目名称简写
                -n) name=$2;shift 2;;#shift 清除 -n 项目名称简写
                -v) version=$2;shift 2;;#shift 清除 -v 项目版本
                -t) port=$2;shift 2;;#shift 清除 -v 项目端口
                -*) echo "error: no such option $1. -h for help";exit 1;; #其它参数认为错误
                *) break;;
        esac
done
echo $name
ps -ef | grep $name | awk '{print $2}' | while read pid
do
   length="`netstat -anp|grep  $pid  |awk '{print $6}' |  wc -l`"
   if [ $length -gt 0 ]; then
        echo pid : $pid
        curl -X POST 127.0.0.1:$port/$name/shutdown
        sleep 3
        kill -9 $pid
   fi
done
mkdir -p /root/data/$name
rm -rf /root/data/$name/$name-$version.jar
cp     /data/workspace/$name.jar  /root/data/$name/$name-$version.jar
nohup /usr/local/java/jdk1.8.0_241/bin/java   -Xms384m -Xmx384m  -jar /root/data/$name/$name-$version.jar  >/dev/null 2>&1 &  

freemem.sh

#! /bin/sh
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /data/memory/logs/mem.log
date >> /data/memory/logs/mem.log
echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /data/memory/logs/mem.log
if [ $free -le 4000 ] ; then
                sync && echo 1 > /proc/sys/vm/drop_caches
                sync && echo 2 > /proc/sys/vm/drop_caches
                sync && echo 3 > /proc/sys/vm/drop_caches
                used_ok=`free -m | awk 'NR==2' | awk '{print $3}'`
                free_ok=`free -m | awk 'NR==2' | awk '{print $4}'`
                echo "Memory usage after | [Use:${used_ok}MB][Free:${free_ok}MB]" >> /data/memory/logs/mem.log
                echo "OK" >> /data/memory/logs/mem.log
else
                echo "Not required" >> /data/memory/logs/mem.log
fi
exit 1

项目创建tag和依据tag回滚

#!/bin/bash

tag=tag_$(date '+%Y%m%d%H%M%S')
project_dir=$3

ck(){
  if [ $? -eq 0 ];then
        echo ''
  else
	echo "有问题,请查看"
        exit
  fi
}


if [ $# -ne 3 ];then
        echo ""
        echo "Usage: "
        echo "       `basename $0` {project} {backTagName} {projectDir}"
        echo ""
else
	read -p "请输入编号: [ 1.创建tag 2.回滚tag ] " num
	case $num in
		1)
			cd $3/$1
			git pull
			git checkout master
			ck
			git tag $tag
			ck
			git push origin $tag
			ck
			echo "创建tag成功,tag名称为:" $tag
			;;
		2)
			cd $3/$1
			git pull
			ck
			git checkout master
			ck
			commit_id=$(git log --format="%h" $2|head -n1)
			git reset --soft $commit_id
			ck
			git push -u origin master -f
			ck
			echo "tag回滚成功"
			;;
		*)
			echo ""
			echo "别瞎鸡儿输!"
			echo ""
	esac
fi


es_back.sh

#!/bin/bash

date=$(date +%Y-%m-%d\ %H:%M:%S)
echo $date >> /soft/scripts/es_back.txt
echo "=========================" >> /soft/scripts/es_back.txt
curl -H "Content-Type: application/json" -XPUT http://172.16.0.5:9220/_snapshot/20220321/data_$(date +%F) >> /soft/scripts/es_back.txt 2>&1
echo "=========================" >> /soft/scripts/es_back.txt

quick_pr.sh

#!/bin/bash
MERGE_BRANCH=$1
TARGET_BRANCH=$2

function error_merge()
{
    if [[ $MERGE_BRANCH = "dev" || $MERGE_BRANCH = "test" || $MERGE_BRANCH = "check" ]];then
		echo "合错了老弟!!!,不能把dev、test、check合并到迭代分支!!!"
		exit 1
    fi
}

function last_status()
{
    if [ $? -eq 0 ]
    then
        return 0
    else
        exit -2
    fi
}

function checkout (){
    if git checkout $1 && git pull
    then
        last_status $1
    fi
}

function merge_branch()
{
	error_merge
    if git checkout $1 && git merge $2 --no-edit
    then
        last_status
    fi
}

function push_branch()
{
    if git push origin $1 && git status
    then
        last_status
    fi
}

function new_line(){
    echo
}


if [ $# -eq 0 ]
then
    echo '请输入要合并的分支名, 用法如下:'
    echo "$0 src_branch dst_branch"
    exit 0
fi

echo -e "\\033[0;31;1m当前摘取的分支是:\\033[0m $MERGE_BRANCH"
echo -e "\\033[0;31;1m要合并到的分支是:\033[0m $TARGET_BRANCH"



read -r -p "是否合并? [Y/n] " input
if  [ $input != 'Y' ] &&[ $input != 'y' ]
then
    echo "取消合并"
    exit 0
fi
new_line


#1.切换开发分支并拉取
echo -e "正在拉取\\033[0;32;1m $MERGE_BRANCH\\033[0m 最新代码"
checkout $MERGE_BRANCH
echo -e "拉取 $MERGE_BRANCH 成功"
new_line

#2.切换到主分支并拉取
echo -e "正在拉取\\033[0;32;1m $TARGET_BRANCH\\033[0m 最新代码"
checkout $TARGET_BRANCH
echo -e "拉取 $TARGET_BRANCH 成功"
new_line

#3.主分支合并到开发分支
echo -e "\\033[0;32;1m $TARGET_BRANCH\\033[0m 正在合并\\033[0;32;1m $MERGE_BRANCH\\033[0m 分支"
merge_branch $TARGET_BRANCH $MERGE_BRANCH
push_branch $TARGET_BRANCH
echo -e "合并取 $TARGET_BRANCH 成功"
new_line

#4.切换到开发分支
echo -e "正在切回\\033[0;32;1m $MERGE_BRANCH\\033[0m 分支"
checkout $MERGE_BRANCH
echo -e "当前以及切回\\033[0;32;1m $MERGE_BRANCH\\033[0m 分支"
new_line

clear_es.sh

list=$(curl --location --request GET 'http://iot.admin.jiaoranyouxuan.com:9201/_cat/indices/jaeger-span*?h=i&s=i:desc' --header 'Authorization: Basic ZWxhc3RpYzpsamgyMDIyLg==')
index=10 # 保留最近的指定数量
for item in $list; do
  if ((index>0)); then
    ((index--))
    echo "跳过索引: $index $item "
    continue
  fi
  res=$(curl --location --request DELETE "http://iot.admin.jiaoranyouxuan.com:9201/$item" --header 'Authorization: Basic ZWxhc3RpYzpsamgyMDIyLg==')
  echo "删除索引: $item $res " # 执行删除语句
done
echo "任务执行完毕..."

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值