当前位置:网站首页>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边栏推荐
- Some considerations on operation / method overloading for thread to release lock resources
- ucore lab4
- js的数组拼接「建议收藏」
- HiTek电源维修X光机高压发生器维修XR150-603-02
- R langage plotly visualisation: visualisation de plusieurs histogrammes normalisés d'ensembles de données et ajout d'une courbe de densité KDE à l'histogramme, réglage de différents histogrammes en ut
- 了解神经网络结构和优化方法
- 为智能设备提供更强安全保护 科学家研发两种新方法
- Object contains copy method?
- Parameters argc and argv of main()
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
猜你喜欢
随机推荐
了解神经网络结构和优化方法
.NET 中的引用程序集
有關二叉樹的一些練習題
Unity - - newtonsoft. Analyse json
Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
Use CAS to complete concurrent operations with atomic variables
小哥凭“量子速读”绝技吸粉59万:看街景图0.1秒,“啪的一下”在世界地图精准找到!...
6月23日《Rust唠嗑室》第三期B站视频地址
有关二叉树的一些练习题
Es update values based on Index Names and index fields
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
openpyxl表格读取实例
QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
隐私计算FATE-离线预测
为智能设备提供更强安全保护 科学家研发两种新方法
基于STM32设计的蓝牙健康管理设备
Some exercises about binary tree
强化学习中好奇心机制
Object contains copy method?








