linux常见shell脚本整理
备份日志
#!/bin/bash
tar -czf log-`date +%Y%m%d`.tar.gz /var/log
00 03 * * 5 /root/logbak.sh
监控内存和磁盘容量,小于给定值时报警
#!/bin/bash
disk_size=$(df / | awk '/\//{print $4}')
mem_size=$(free | awk '/Mem/{print $4}')
while :
do
if [ $disk_size -le 512000 -a $mem_size -le 1024000 ]
then
mail ‐s "Warning" root <<EOF
Insufficient resources,资源不足
EOF
fi
done
监控网段中的主机状态
#!/bin/bash
myping(){
ping ‐c 2 ‐i 0.3 ‐W 1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {1..254}
do
myping 192.168.4.$i &
done
根据文件创建用户名密码
#!/bin/bash
for i in `cat user.txt`
do
useradd $i
echo "123456" | passwd ‐‐stdin $i
done
查看连接本机的Ip
#!/bin/bash
netstat -atn | awk '{print $5}' | awk '{print $1}' | sort -nr | uniq -c
统计时间段内访问量
#!/bin/bash
awk -F "[ /:]" '$7":"$8>="13:30" && $7":"$8<="14:30"' /var/log/httpd/access_log |wc -l
awk -F "[ /:]" '$7":"$8>="13:30" && $7":"$8<="14:30"{print $1}' /var/log/httpd/access_log
Nginx启动脚本
#!/bin/bash
program=/usr/local/nginx/sbin/nginx
pid=/usr/local/nginx/logs/nginx.pid
start(){
if [ -f $pid ];then
echo "nginx 服务已经处于开启状态"
else
$program
fi
stop(){
if [ -! -f $pid ];then
echo "nginx 服务已经关闭"
else
$program -s stop
echo "关闭服务 ok"
fi
}
status(){
if [ -f $pid ];then
echo "服务正在运行..."
else
echo "服务已经关闭"
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
stop
sleep 1
start;;
status)
status;;
*)
echo "你输入的语法格式错误"
esac
自动切割nginx日志文件
logs_path="/usr/local/nginx/logs/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
0 1 * * * /data/scripts/nginx_log.sh
检测Mysql数据库连接数量
#!/bin/bash
log_file=/var/log/mysql_count.log
user=root
passwd=123456
while :
do
sleep 2
count=`mysqladmin -u "$user" -p "$passwd" status | awk '{print $4}'`
echo "`date +%Y‐%m‐%d` 并发连接数为:$count" >> $log_file
done
检测mysql服务是否存活
#!/bin/bash
host=192.168.51.198
user=root
passwd=123456
mysqladmin -h '$host' -u '$user' -p'$passwd' ping &>/dev/null
if [ $? -eq 0 ]
then
echo "MySQL is UP"
else
echo "MySQL is down"
fi
备份Mysql
#!/bin/bash
user=root
passwd=123456
dbname=mysql
date=$(date +%Y%m%d)
[ ! -d /mysqlbackup ] && mkdir /mysqlbackup
mysqldump -u "$user" -p "$passwd" "$dbname" > /mysqlbackup/"$dbname"-${date}.sql
根据md5校验码,检测文件是否修改
#!/bin/bash
for i in $(ls /etc/*.conf)
do
md5sum "$i" >> /var/log/conf_file.log
done
非交互生成ssh秘钥文件
#!/bin/bash
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh‐keygen -t RSA -N '' -f ~/.ssh/id_rsa
监控http服务器状态
#!/bin/bash
url=http://http://183.232.231.172/index.html
check_http()
{
status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
}
while :
do
check_http
date=$(date +%Y%m%d‐%H:%M:%S)
echo "当前时间为:$date
$url 服务器异常,状态码为${status_code}.
请尽快排查异常." > /tmp/http$$.pid
if [ $status_code -ne 200 ];then
mail -s Warning root < /tmp/http$$.pid
else
echo "$url 连接正常" >> /var/log/http.log
fi
sleep 5
done
删除目录下大小为0的文件
#!/bin/bash
dir="/var/www/html"
find $dir -type f -size 0 -exec rm -rf {} \;
查找linux系统中的僵尸进程
#!/bin/bash
ps aux | awk '{if($8 == "Z"){print $2,$11}}'
生成随机密码
#!/bin/bash
tr -dc '_A‐Za‐z0‐9' </dev/urandom | head -c 10
生成随机密码(指定字符串)
key="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
num=${#key}
pass=''
for i in {1..8}
do
index=$[RANDOM%num]
pass=$pass${key:$index:1}
done
echo $pass
获取本地的一些信息
#!/bin/bash
ip a s | awk 'BEGIN{print " 本 机 MAC 地 址 信 息 如 下 :"}/^[0‐9]/{print $2;getline;if($0~/link\/ether/){print $2}}' | grep -v lo:
ss -nutlp | awk '{print $1,$5}' | awk -F"[: ]" '{print "协议:"$1,"端口号:"$NF}' | grep "[0‐9]" | uniq
修改linux系统最大打开文件数量
#!/bin/bash
cat >> /etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 65536
EOF
一次部署memcache
#!/bin/bash
wget http://www.memcached.org/files/memcached-1.5.1.tar.gz
yum -y install gcc
tar -xf memcached‐1.5.1.tar.gz
cd memcached‐1.5.1
./configure
make
make install
打印各种格式的时间
echo "显示星期简称(如:Sun)"
date +%a
echo "显示星期全称(如:Sunday)"
date +%A
echo "显示月份简称(如:Jan)"
date +%b
echo "显示月份全称(如:January)"
date +%B
echo "显示数字月份(如:12)"
date +%m
echo "显示数字日期(如:01 号)"
date +%d
echo "显示数字年(如:01 号)"
date +%Y echo "显示年‐月‐日"
date +%F
echo "显示小时(24 小时制)"
date +%H
echo "显示分钟(00..59)"
date +%M
echo "显示秒"
date +%S
echo "显示纳秒"
date +%N
echo "组合显示"
date +"%Y%m%d %H:%M:%S"
生成签名秘钥和证书
read -p "请输入存放证书的目录:" dir
if [ ! -d $dir ];then
echo "该目录不存在"
exit
fi
read -p "请输入密钥名称:" name
openssl genrsa -out ${dir}/${name}.key
openssl req -new -x509 -key ${dir}/${name}.key -subj "/CN=common" -out ${dir}/${name}.crt
定时启动Python程序
- 需要保证job.py文件可执行
- chmod u+x /root/s1.sh 保证sh文件的执行权限
- crontab -e 编辑定时任务
- */1 * * * * /root/s1.sh # 设置每分钟执行一次
#!/bin/bash
python3 /root/job.py
echo 'Python脚本执行成功,时间:' >> /root/log
date >> /root/log
开启和关闭jar操作
- jar包名称为 uid-consumer-1.1.0-SNAPSHOT.jar
#!/bin/sh
APP_NAME=uid-consumer
SERVER_PORT=9999
JAR_PATH='/app/uid-consumer'
JAR_NAME=uid-consumer-1.1.0-SNAPSHOT.jar
JAR_PID=$JAR_NAME\.pid
LOG_FILE=logs
JAVA_OPTS="-Xms512m -Xmx512m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m -XX:ParallelGCThreads=4 -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8"
is_exist() {
pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 0
else
return 1
fi
}
start() {
is_exist
if [ $? -eq "1" ]; then
echo "$APP_NAME is already running pid is ${pid}"
else
nohup java $JAVA_OPTS -Xloggc:$LOG_FILE/gc/gclog.log -XX:HeapDumpPath=$LOG_FILE/gc/HeapDump.hprof -jar $JAR_PATH/$JAR_NAME >./$LOG_FILE/run.log 2>&1 &
echo $! > $JAR_PID
echo "start $APP_NAME successed pid is $! "
tail -1000f $LOG_FILE/run.log
fi
}
stop() {
pidf=$(cat $JAR_PID)
echo "pid = $pidf begin kill $pidf"
kill $pidf
rm -rf $JAR_PID
sleep 2
is_exist
if [ $? -eq "1" ]; then
echo "pid = $pid begin kill -9 $pid"
kill -9 $pid
sleep 2
echo "$APP_NAME process stopped!"
else
echo "$APP_NAME is not running!"
fi
}
status() {
is_exist
if [ $? -eq "1" ]; then
echo "$APP_NAME is running,pid is ${pid}"
else
echo "$APP_NAME is not running!"
fi
}
restart() {
stop
start
}
usage() {
echo "Usage: sh run-service.sh [ start | stop | restart | status ]"
exit 1
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
restart
;;
'status')
status
;;
*)
usage
;;
esac
exit 0