当前位置:网站首页>Reorganize common shell scripts for operation and maintenance frontline work

Reorganize common shell scripts for operation and maintenance frontline work

2022-06-27 09:40:00 BOGO

Hello, everyone ! I'm BOGO !


1、 monitoring Nginx Access log 502 situation , And do the corresponding action

Suppose the server environment is lnmp, Recent visits often appear 502 The phenomenon , And 502 Error in restart php-fpm Disappear after service , So you need to write monitoring scripts , Once it appears 502, It will restart automatically php-fpm service .

# scene :
#1. The path to access the log file :/data/log/access.log
#2. Script loop , Every time 10 Check once per second ,10 The number of logs per second is 300 strip , appear 502 Not less than 10%(30 strip ) You need to restart php-fpm service 
#3. The restart command is :/etc/init.d/php-fpm restart
#!/bin/bash
###########################################################
# monitoring Nginx Access log 502 situation , And do the corresponding action 
###########################################################
log=/data/log/access.log
N=30 # Set threshold 
while :do
 # Check the access log for the latest 300 strip , And statistics 502 The number of times 
    err=`tail -n 300 $log |grep -c '502" '` 
if [ $err -ge $N ] 
then
/etc/init.d/php-fpm restart 2> /dev/null 
# Set up 60s Delay prevention scripts bug Cause infinite restart php-fpm service 
     sleep 60
 fi
 sleep 10
 done

2、 Assign the results to variables

Application scenarios : You want to assign execution results or location parameters to variables , For later use .

Method 1:

for i in $(echo "4 5 6"); do
   eval a$i=$idone
echo $a4 $a5 $a6

Method 2: Set the position parameter 192.168.1.1{1,2} Split into variables

num=0
for i in $(eval echo $*);do   #eval take {1,2} Decompose into 1 2
   let num+=1
   eval node${num}="$i"
done
echo $node1 $node2 $node3
# bash a.sh 192.168.1.1{1,2}
192.168.1.11 192.168.1.12

 Method 3:arr=(4 5 6)
INDEX1=$(echo ${arr[0]})
INDEX2=$(echo ${arr[1]})
INDEX3=$(echo ${arr[2]})

3、 Batch modify file name

Example :

# touch article_{1..3}.html
# lsarticle_1.html  article_2.html  article_3.html
 Purpose : hold article Change it to bbs

Method 1:

for file in $(ls *html); do
    mv $file bbs_${file#*_}
    # mv $file $(echo $file |sed -r 's/.*(_.*)/bbs\1/')
    # mv $file $(echo $file |echo bbs_$(cut -d_ -f2)

Method 2:


for file in $(find . -maxdepth 1 -name "*html"); do
     mv $file bbs_${file#*_}done

Method 3:

# rename article bbs *.html
 Delete the first five lines of a document that contain letters , At the same time to delete 6 To 10 All the letters contained in the row 

1) Prepare test files , The file named 2.txt

 The first 1 That's ok 1234567 No letters 
 The first 2 That's ok 56789BBBBBB
 The first 3 That's ok 67890CCCCCCCC
 The first 4 That's ok 78asdfDDDDDDDDD
 The first 5 That's ok 123456EEEEEEEE
 The first 6 That's ok 1234567ASDF
 The first 7 That's ok 56789ASDF
 The first 8 That's ok 67890ASDF
 The first 9 That's ok 78asdfADSF
 The first 10 That's ok 123456AAAA
 The first 11 That's ok 67890ASDF
 The first 12 That's ok 78asdfADSF
 The first 13 That's ok 123456AAAA

2) The script is as follows :

#!/bin/bash
###############################################################
 Delete the first five lines of a document that contain letters , At the same time to delete 6 To 10 All the letters contained in the row 
##############################################################
sed -n '1,5'p 2.txt |sed '/[a-zA-Z]/'d
sed -n '6,10'p 2.txt |sed s'/[a-zA-Z]//'g
sed -n '11,$'p 2.txt
# The end result is just a print on the screen , If you want to change the file directly , The output can be written to a temporary file , Replace with 2.txt Or use -i Options 

4、 Count the current directory with .html Total size of files at the end

Method 1:

# find . -name "*.html" -exec du -k {} \; |awk '{sum+=$1}END{print sum}'

 Method 2:
```bash
for size in $(ls -l *.html |awk '{print $5}'); do
    sum=$(($sum+$size))
done
echo $sum

5、 Scan host port status

#!/bin/bash
HOST=$1
PORT="22 25 80 8080"
for PORT in $PORT; do
    if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
        echo "$PORT open"
    else
        echo "$PORT close"
    fi
done
 use  shell  The number of letters in the print sample statement is less than 6 's words 

# Sample statements :
#Bash also interprets a number of multi-character options.
#!/bin/bash
##############################################################
#shell The number of letters in the print sample statement is less than 6 's words 
##############################################################
for s in Bash also interprets a number of multi-character options.
do
 n=`echo $s|wc -c` 
 if [ $n -lt 6 ] 
 then
 echo $s
 fi
done

6、 Enter the number and run the corresponding command

#!/bin/bash
##############################################################
# Enter the number and run the corresponding command 
##############################################################
echo "*cmd menu* 1-date 2-ls 3-who 4-pwd 0-exit "
while :
do
# Capture the value the user typed 
 read -p "please input number :" n
 n1=`echo $n|sed s'/[0-9]//'g`
# Null input detection  
 if [ -z "$n" ]
 then
 continue
 fi
# Non digital input detection  
 if [ -n "$n1" ]
 then
 exit 0
 fi
 break
done
case $n in
 1)
 date
 ;;
 2)
 ls
 ;;
 3)
 who
 ;;
 4)
 pwd
 ;;
 0)
 break
 ;;
    # Enter the number not 1-4 A hint of 
 *)
 echo "please input number is [1-4]"
esac
原网站

版权声明
本文为[BOGO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270932393873.html