当前位置:网站首页>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边栏推荐
- Decompile the jar package and recompile it into a jar package after modification
- Only one ConfirmCallback is supported by each RabbitTemplate 解决办法
- Rockermq message sending mode
- Principle and application of the most complete H-bridge motor drive module L298N
- Source insight 工具安装及使用方法
- Quartz (timer)
- Imx8qxp DMA resources and usage (unfinished)
- .NET 中的引用程序集
- 提高效率 Or 增加成本,开发人员应如何理解结对编程?
- ucore lab5
猜你喜欢

Bluetooth health management device based on stm32

QT运行显示 This application failed to start because it could not find or load the Qt platform plugin

有關二叉樹的一些練習題

Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher

.NET 中的引用程序集

Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])
![文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument

Reading and writing Apache poi

The markdown plug-in of the browser cannot display the picture
随机推荐
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
Quartz(定时器)
Five page Jump methods for wechat applet learning
ucore lab4
提高效率 Or 增加成本,开发人员应如何理解结对编程?
【STM32】HAL库 STM32CubeMX教程十二—IIC(读取AT24C02 )
谷歌浏览器 chropath插件
ucore lab3
Vector:: data() pointer usage details
新旧两个界面对比
Video file too large? Use ffmpeg to compress it losslessly
One week's experience of using Obsidian (configuration, theme and plug-in)
R语言使用econocharts包创建微观经济或宏观经济图、demand函数可视化需求曲线(demand curve)、自定义配置demand函数的参数丰富可视化效果
R语言plotly可视化:可视化多个数据集归一化直方图(historgram)并在直方图中添加密度曲线kde、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图
unity--newtonsoft.json解析
集合框架 泛型LinkedList TreeSet
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
How do I get the STW (pause) time of a GC (garbage collector)?
Markem imaje马肯依玛士喷码机维修9450E打码机维修
Process 0, process 1, process 2