当前位置:网站首页>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边栏推荐
- 多个类的设计
- 【SO官方采访】为何使用Rust的开发者如此深爱它
- Only one confirmcallback is supported by each rabbittemplate
- Curiosity mechanism in reinforcement learning
- Apache POI的读写
- 微信小程序学习之五种页面跳转方法.
- openpyxl表格读取实例
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- ucore lab5
- Flow chart of Alipay wechat payment business
猜你喜欢

Tdengine invitation: be a superhero who uses technology to change the world and become TD hero

Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02

ucore lab5

Markem imaje马肯依玛士喷码机维修9450E打码机维修

leetcode:968. 监控二叉树【树状dp,维护每个节点子树的三个状态,非常难想权当学习,类比打家劫舍3】

How do I get the STW (pause) time of a GC (garbage collector)?

There is no doubt that this is an absolutely elaborate project

反编译jar包,修改后重新编译为jar包

ucore lab3

Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
随机推荐
Prometheus alarm process and related time parameter description
Rockermq message sending and consumption mode
R语言plotly可视化:plotly可视化基础小提琴图(basic violin plot in R with plotly)
[diffusion model]
ucore lab5
R language plot visualization: visualize the normalized histograms of multiple data sets, add density curve KDE to the histograms, set different histograms to use different bin sizes, and add edge whi
10 常见网站安全攻击手段及防御方法
unity--newtonsoft.json解析
Markem imaje马肯依玛士喷码机维修9450E打码机维修
【系统设计】邻近服务
Understand neural network structure and optimization methods
One week's experience of using Obsidian (configuration, theme and plug-in)
Privacy computing fat offline prediction
Hitek power supply maintenance X-ray machine high voltage generator maintenance xr150-603-02
你睡觉时大脑真在自动学习!首个人体实验证据来了:加速1-4倍重放,深度睡眠阶段效果最好...
Use aspese slides to convert PPT to PDF
Quelques exercices sur les arbres binaires
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
Collection framework generic LinkedList TreeSet
This application failed to start because it could not find or load the QT platform plugin