shell脚本合集

1. 修改postgresql数据库ip

#!/bin/bash

#填写T端外网网卡名
Toutnetwork=ens33

#填写T端内网网卡名
Tinnetwork=ens34

#填写T端外网参数
Tout=$(grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-$Toutnetwork | awk -F '=' '{print $2}')
Toutnetmask=$(grep 'NETMASK' /etc/sysconfig/network-scripts/ifcfg-$Toutnetwork | awk -F '=' '{print $2}')
Toutgateway=$(grep 'GATEWAY' /etc/sysconfig/network-scripts/ifcfg-$Toutnetwork | awk -F '=' '{print $2}')


#填写T端内网参数
Tin=$(grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-$Tinnetwork | awk -F '=' '{print $2}')
Tinnetmask=$(grep 'NETMASK' /etc/sysconfig/network-scripts/ifcfg-$Tinnetwork | awk -F '=' '{print $2}')
Tingateway=$(grep 'GATEWAY' /etc/sysconfig/network-scripts/ifcfg-$Tinnetwork | awk -F '=' '{print $2}')


#填写U端外网参数
Uout=116.207.128.114
Uoutnetmask=255.255.255.252
Uoutgateway=116.207.128.113


#填写U端内网参数
Uin=4.250.240.2
Uinnetmask=255.255.255.0
Uingateway=4.250.240.10


env PGPASSWORD=whyh2021 psql -h127.0.0.1 -p5432 -Upostgres -ddecdb --command "UPDATE "public"."eth" SET "net_card" = 'enp4s0f0', "ipv4_netmask" = '$Toutnetmask', "ipv4_gateway" = '$Toutgateway', "virtual_ip" = '$Tout', "netcard_direct" = 'out', "terminal" = 'T', "ipv4_ip" = '$Tout' WHERE "netcard_direct" = 'out' AND "terminal" = 'T' ;"

env PGPASSWORD=whyh2021 psql -h127.0.0.1 -p5432 -Upostgres -ddecdb --command "UPDATE "public"."eth" SET "net_card" = 'enp4s0f1', "ipv4_netmask" = '$Tinnetmask', "ipv4_gateway" = '$Tingateway', "virtual_ip" = '$Tin', "netcard_direct" = 'in', "terminal" = 'T', "ipv4_ip" = '$Tin' WHERE "netcard_direct" = 'in' AND "terminal" = 'T';"

env PGPASSWORD=whyh2021 psql -h127.0.0.1 -p5432 -Upostgres -ddecdb --command "UPDATE "public"."eth" SET "net_card" = 'enp4s0f0', "ipv4_netmask" = '$Uoutnetmask', "ipv4_gateway" = '$Uoutgateway', "virtual_ip" = '$Uout', "netcard_direct" = 'out', "terminal" = 'U', "ipv4_ip" = '$Uout' WHERE "netcard_direct" = 'out' AND "terminal" = 'U';"

env PGPASSWORD=whyh2021 psql -h127.0.0.1 -p5432 -Upostgres -ddecdb --command "UPDATE "public"."eth" SET "net_card" = 'enp4s0f1', "ipv4_netmask" = '$Uinnetmask', "ipv4_gateway" = '$Uingateway', "virtual_ip" = '$Uin', "netcard_direct" = 'in', "terminal" = 'U', "ipv4_ip" = '$Uin' WHERE "netcard_direct" = 'in' AND "terminal" = 'U';"

2. tomcat启动 关闭 重启

[root@localhost bin]# cat test.sh 
#!/bin/bash

sdir=$(cd "$(dirname "$0")"; pwd)

echo -e "\033[1;32;40m 1.启动 \033[0m \033[1;32;40m 2.关闭 \033[0m \033[1;32;40m 3.重启 \033[0m"
read -p "请选择你要执行的操作:" num

function start() {
    
    
    bash $sdir/startup.sh && echo -e "\033[1;32;40m 启动成功!!! \033[0m"
}

function stop() {
    
    
    process=$(ps -ef | grep $sdir | grep -v grep | awk '{print $2}')
    kill -9 $process &>/dev/null && echo -e "\033[1;32;40m 关闭成功!!!\033[0m"
}

function restart() {
    
    
    process=$(ps -ef | grep $sdir | grep -v grep | awk '{print $2}')
    kill -9 $process &>/dev/null
    bash $sdir/startup.sh && echo -e "\033[1;32;40m 重启成功!!!\033[0m"
}

if [ $num -eq 1 ] &>/dev/null
    then
        start
elif [ $num -eq 2 ] &>/dev/null
    then
        stop
elif [ $num -eq 3 ] &>/dev/null
    then
        restart
    else
        echo -e "\033[1;32;40m 输入有误请检查!提示1-3 \033[0m" 
fi

[root@localhost bin]# 

3. 每2分钟将系统负载情况写入到日志

[root@localhost scripts]# cat system_load.sh 
#!/bin/bash

load1=$(uptime | awk -F'[ ,]+' '{print $9}')
load5=$(uptime | awk -F'[ ,]+' '{print $10}')
load15=$(uptime | awk -F'[ ,]+' '{print $11}')
time=$"`date +"%Y%m%d %H:%M:%S"`"
#日志目录位置
dir=/scripts/log

cat >> $dir << EOF
当前系统时间: $time
1分钟的系统平均负载:$load1
5分钟的系统平均负载:$load5
15分钟的系统平均负载:$load15
==========================
EOF
[root@localhost scripts]# 
[root@localhost scripts]# crontab -l
*/2 * * * * /bin/sh /scripts/system_load.sh

4. 删除日志脚本

[etl@etl1 ~]$ cat /home/etl/script/move_bakktr.sh 
#move the backed ktr files 
####huawei 20191231 add
JOBS='/jobs'
JOBD='/home/etl/jobs_updatebak'
###bak ktr dir
for I in `find $JOBS  -type d -iname "*.bak20[1-3][0-9][0,1][0-9][0-3][0-9]"`
do

DP=${I/$JOBS/$JOBD}
DIRD=`dirname $DP`

[ -d $DIRD  ]||mkdir -p $DIRD
mv $I $DP

done
###bak ktr file 
for I in `find $JOBS  -type f -iname "*.ktr20[1-3][0-9][0,1][0-9][0-3][0-9]"`
do

DP=${I/$JOBS/$JOBD}
DIRD=`dirname $DP`

[ -d $DIRD  ]||mkdir -p $DIRD
mv $I $DP

done

for I in `find $JOBS  -type f -iname "*.kjb20[1-3][0-9][0,1][0-9][0-3][0-9]"`
do

DP=${I/$JOBS/$JOBD}
DIRD=`dirname $DP`

[ -d $DIRD  ]||mkdir -p $DIRD
mv $I $DP

done
[etl@etl1 ~]$ 

5. tomcat日志切割脚本

[root@localhost logs]# cat /scripts/backup_catalina.sh 
#!/bin/bash

#日志路径
catalina_dir=/usr/local/apache-tomcat-7.0.109/logs

#备份日志到新的文件中
cp $catalina_dir/catalina.out $catalina_dir/catalina`date +"%Y%m%d"`.log

#清空tomcat默认日志输出文件
echo "" > $catalina_dir/catalina.out

#查看并清理不需要的文件
find $catalina_dir -mtime +7 -name "catalina_20[1-3][0-9][0,1][0-9][0-3][0-9].log" -exec rm -rf {
    
    } \;

#如果有多个tomcat复制即可

猜你喜欢

转载自blog.csdn.net/qq_49530779/article/details/125406641