整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新!

Posted 爱是与世界平行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新!相关的知识,希望对你有一定的参考价值。

整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新!

一、ffmpeg脚本

1.1 打开进程,并判断进程数量

原版本:

#!/bin/bash
ffmpegPid = $( ps -ef | grep -E 'ffmpeg.*$1$2$3' | grep -v 'grep' | awk 'print $2')
if [ -z "$ffmpegPid" ]
then
	threadNum = $( ps -ef | grep ffmpeg | grep -v 'grep' | wc -l | awk 'print $1')
	if [$threadNum -gt 10]
	then
		# 线程数大于10个,需要干掉ffmpeg线程
		echo threadNum too more!
		for thread in $threadNum
		do
			kill -9 $thread
		done
	else
		nohup ffmpeg -re  -rtsp_transport tcp -i "rtsp://ip:port/dss/monitor/params?cameraid=$1%24$2&substream=$3" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 $4 > /Users/chuxiaowei/ffmpeg$1$2$3.log 2>&1 &
		ps -ef | grep -E 'ffmpeg.*$1$2$3' | grep -v 'grep' | awk 'print $2'
	fi
else
	ps -ef | grep -E 'ffmpeg.*$1$2$3' | grep -v 'grep' | awk 'print $2'
fi

改进之后:

#!/bin/bash
ffmpegPid = $( ps -ef | grep -E 'ffmpeg.*$1$2$3' | grep -v 'grep' | awk 'print $2')
if [ -z "$ffmpegPid" ]
then
    nohup ffmpeg -re  -rtsp_transport tcp -i "rtsp://ip:port/dss/monitor/params?cameraid=$1%24$2&substream=$3" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 $4 > /root/ffmpeg$1$2$3.log 2>&1 &
    ps -ef | grep -E 'ffmpeg.*$4*' | grep -v 'grep' | awk 'print $2'
fi

1.2 关闭进程

#关闭进程
#!/bin/bash
ps -ef | grep -E 'ffmpeg.*$1*' | grep -v 'grep' | awk 'print $2' | xargs kill

二、java jar包启动-剔除Pom中依赖

#!/bin/bash
pid=$(ps -ef | grep java | grep -E '*rtsptortmp.*' | awk 'print $2')
echo "pid = $pid"
if [ $pid ];then
    kill -9 $pid
    echo "kill the process rtsptortmp pid = $pid"
fi

nohup java -Dloader.path=/root/rtsptortmplib -jar rtsptortmp-1.0-SNAPSHOT.jar --spring.profiles.active=prod > /root/logs/rtsptortmp.log 2>&1 &
tail -f /root/logs/rtsptortmp.log

三、Java jar包通用启动脚本

eg:
./start.sh java.jar

#!/bin/bash
pid=$(ps -ef | grep java | grep -E '*$1*' | awk 'print $2')
echo "pid = $pid"
if [ $pid ];then
    kill -9 $pid
    echo "kill the process pid = $pid"
fi

nohup java -jar -Xms256m -Xmx256m $1 --spring.profiles.active=prod > /root/logs/$1.log 2>&1 &
tail -f /root/logs/$1.log

四、查看centos配置信息脚本

#!/bin/bash

echo ======= cpu个数: =======
grep 'physical id' /proc/cpuinfo | sort -u | wc -l

echo ======= cpu核数: =======
cat /proc/cpuinfo | grep "cpu cores" | uniq

echo ======= cpu型号: =======
cat /proc/cpuinfo | grep 'model name' |uniq

echo ======= cpu内核频率: =======
cat /proc/cpuinfo |grep MHz|uniq

echo ======= cpu统计信息: =======
lscpu

echo ======= 内存总数: =======
cat /proc/meminfo | grep MemTotal

echo ======= 内核版本: =======
cat /proc/version

echo ======= 操作系统内核信息: =======
uname -a

echo ======= 磁盘信息: =======
fdisk -l

五、Jenkins项目打包发布脚本

#!/bin/bash

//传入的war包名称
name=$1 	
//war包所在目录							
path=$2
//上传的war包位置				
path_w=$3		
//如果项目正在运行就杀死进程
if [ -f "$path/$name" ];then
	echo "delete the file $name"
	rm -f  $path/$name
else
	echo "the file $name is not exist"
fi
//把jenkins上传的war包拷贝到我们所在目录 
cp $path_w/$name $path/
echo "copy the file $name from $path_w to $path"

//获取该项目正在运行的pid			
pid=$(ps -ef | grep java | grep $name | awk 'print $2')
echo "pid = $pid"
//如果项目正在运行就杀死进程
if [ $pid ];then
	kill -9 $pid
	echo "kill the process $name pid = $pid"
else
	echo "process is not exist"
fi
//要切换到项目目录下才能在项目目录下生成日志
cd $path
//防止被jenkins杀掉进程 BUILD_ID=dontKillMe
BUILD_ID=dontKillMe
//启动项目
nohup java -server -Xms256m -Xmx512m -jar -Dserver.port=20000 $name >> nohup.out 2>&1 &
//判断项目是否启动成功
pid_new=$(ps -ef | grep java | grep $name | awk 'print $2')
if [ $? -eq 0 ];then
echo "this application  $name is starting  pid_new = $pid_new"
else 
echo "this application  $name  startup failure"
fi
echo $! > /var/run/myClass.pid
echo "over"

六、mysql-使用脚本进行分库分表备份

[root@ctos3 ~]# cat bak.sh
#!/bin/bash

MYUSER="root"
MYPASS="guoke123"
MYLOG="mysql -u$MYUSER -p$MYPASS -e"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS -x -F"
DBLIST=$($MYLOG  "show databases;" | sed 1d | grep -Ev 'info|mysq|per|sys')
DIR=/backup
[ ! -d $DIR ] && mkdir $DIR
cd $DIR

for dbname in $DBLIST
do
 TABLIST=$($MYLOG "show tables from $dbname;" | sed 1d)
for tabname in $TABLIST
do
  mkdir -p $DIR/$dbname
   $MYDUMP $dbname $tabname  --events |gzip > $DIR/$dbname/$tabname_$(date +%F_%T).sql.gz
done
done

七、实时监控网卡流量的通用脚本

[root@ceph-node1 ~]# cat /root/net_monit.sh
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
export PATH
function traffic_monitor 
 
  OS_NAME=$(sed -n '1p' /etc/issue)
 
  eth=$1
 
  if [ ! -d /sys/class/net/$eth ];then
      echo -e "Network-Interface Not Found"
      echo -e "You system have network-interface:\\n`ls /sys/class/net`"
      exit 5
  fi
  while [ "1" ]
  do
    
    STATUS="fine"
   
    RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk 'print $2')
    TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk 'print $10')
 
    sleep 1
    RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk 'print $2')
    TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk 'print $10')
    clear
  
    RX=$(($RXnext-$RXpre))
    TX=$(($TXnext-$TXpre))
 
    if [[ $RX -lt 1024 ]];then
      RX="$RXB/s"
    elif [[ $RX -gt 1048576 ]];then
      RX=$(echo $RX | awk 'print $1/1048576 "MB/s"')
      $STATUS="busy"
    else
      RX=$(echo $RX | awk 'print $1/1024 "KB/s"')
    fi
 
    if [[ $TX -lt 1024 ]];then
      TX="$TXB/s"
      elif [[ $TX -gt 1048576 ]];then
      TX=$(echo $TX | awk 'print $1/1048576 "MB/s"')
    else
      TX=$(echo $TX | awk 'print $1/1024 "KB/s"')
    fi
   
    echo -e "==================================="
    echo -e "Welcome to Traffic_Monitor stage"
    echo -e "version 1.0"
    echo -e "Since 2018.7.2"
    echo -e "Created by wangshibo"
    echo -e "BLOG: http://www.cnblogs.cn/kevingrace"
    echo -e "==================================="
    echo -e "System: $OS_NAME"
    echo -e "Date:   `date +%F`"
    echo -e "Time:   `date +%k:%M:%S`"
    echo -e "Port:   $1"
    echo -e "Status: $STATUS"
    echo -e  " \\t     RX \\tTX"
    echo "------------------------------"
    
    echo -e "$eth \\t $RX   $TX "
    echo "------------------------------"
 
    echo -e "Press 'Ctrl+C' to exit"
  done

 
if [[ -n "$1" ]];then
  traffic_monitor $1
else
  echo -e "None parameter,please add system netport after run the script! \\nExample: 'sh traffic_monitor eth0'"
fi
 
 
 
[root@ceph-node1 ~]# chmod 755 /root/net_monit.sh
[root@ceph-node1 ~]# sh /root/net_monit.sh eth0               #eth0是网卡设备名称,如果是网卡绑定bond0,后面就跟bond0
===================================
Welcome to Traffic_Monitor stage
version 1.0
Since 2018.7.2
Created by wangshibo
BLOG: http://www.cnblogs.cn/kevingrace
===================================
System: CentOS release 6.9 (Final)
Date:   2018-07-02
Time:   15:19:34
Port:   eth0
Status: fine
        RX  TX
------------------------------
eth0   417B/s   390B/s
------------------------------
Press 'Ctrl+C' to exit
===================================
Welcome to Traffic_Monitor stage
version 1.0
Since 2018.7.2
Created by wangshibo
BLOG: http://www.cnblogs.cn/kevingrace
===================================
System: CentOS release 6.9 (Final)
Date:   2018-07-02
Time:   15:19:35
Port:   eth0
Status: fine
        RX  TX
------------------------------
eth0   1.49902KB/s   1.3252KB/s
------------------------------
Press 'Ctrl+C' to exit

八、监控磁盘的监控脚本

[root@ceph-node1 ~]# cat disk_monit.sh
#!/bin/bash
#filename:Monitor_Disk
Monitor_Disk()
mkdir -p /mnt/Monitor_Disk
fdisk -l|grep "Disk /dev/" | awk 'print $2,$3$4'|tr -d ',:'>/mnt/Monitor_Disk/device_list.log
N=1;ECHO 90
while read device_line
do
Device=`echo $device_line|awk 'print $1'`
Sum=`echo $device_line|awk 'print $2'`
df -h |grep "$Device"|sort>/mnt/Monitor_Disk/$N_partitions.log
echo
echo "** 第$N块硬盘($Device):$Sum **"| grep -E "$Device|$Sum|$N" --color=yes
echo "------------------------------------"
echo -e "linux分区 挂载目录 总大小 已用 剩余 已用百分比 文件系统 ID system  \\
块大小  预留空间  ">/mnt/Monitor_Disk/$N_Over.log
echo -e "========= ======== =====  ===  ===  ==========  ======= == ======  \\
======  ======== ">>/mnt/Monitor_Disk/$N_Over.log
Num_Partition=`cat /mnt/Monitor_Disk/$N_partitions.log|wc -l`
n=0
while read partition_line
do
    Partition_Name=`echo $partition_line|awk 'print $1'`
    Mount_Dir=`echo $partition_line|awk 'print $6'`
    Partition_Sum=`echo $partition_line|awk 'print $2'`
    Partition_Used=`echo $partition_line|awk 'print $3'`
    Partition_Leave=`echo $partition_line| awk 'print $4'`
    Partition_Percent=`echo $partition_line|awk 'print $5'`
    Partition_Type=`mount|grep $Partition_Name|awk 'print $5$6'`
    Partition_Id=`fdisk -l | grep $Partition_Name|tr -d '\\*'|awk 'print $5'`
    Partition_System=`fdisk -l | grep $Partition_Name|tr -d '\\*'|awk 'print $6'`
    Part_Block_Size_B=`tune2fs -l $Partition_Name|grep "Block size"|awk 'print $3'`
    Part_Lift_For_Root_Blocks=`tune2fs -l $Partition_Name|grep "Reserved block count:"|\\
    awk 'print $4'`
    Part_Block_Size=`echo $Part_Block_Size_B/1024|bc`
    Part_Lift_For_Root=`echo $Part_Lift_For_Root_Blocks*$Part_Block_Size/1024|bc`
    echo -e "$Partition_Name $Mount_Dir $Partition_Sum $Partition_Used $Partition_Leave \\
$Partition_Percent $Partition_Type  $Partition_Id $Partition_System  \\
$Part_Block_SizeK $Part_Lift_For_RootM">>/mnt/Monitor_Disk/$N_Over.log
    let n++
 
    [ $n -eq $Num_Partition ]&&(cat /mnt/Monitor_Disk/$N_Over.log|column -t;echo)
done</mnt/Monitor_Disk/$N_partitions.log
let N++
done</mnt/Monitor_Disk/device_list.log
ECHO 90
rm -fr /mnt/Monitor_Disk

ECHO()
for ((i=1;i<=$1;i++))
do
echo -n "#"
[ $i -eq $1 ]&&(echo;echo)
done

Monitor_Disk
 
 
[root@ceph-node1 ~]# chmod 755 disk_monit.sh
[root@ceph-node1 ~]# sh disk_monit.sh
##########################################################################################
 
 
** 第1块硬盘(/dev/sdb):577.4GB **
------------------------------------
linux分区  挂载目录  总大小  已用  剩余  已用百分比  文件系统  ID  system  块大小  预留空间
=========  ========  =====   ===   ===   ==========  =======   ==  ======  ======  ========
/dev/sdb1  /data     530G    42G   461G  9%          ext4(rw)  83  Linux   4K      27532M
 
 
** 第2块硬盘(/dev/sda):322.1GB **
------------------------------------
linux分区  挂载目录  总大小  已用  剩余  已用百分比  文件系统  ID  system  块大小  预留空间
=========  ========  =====   ===   ===   ==========  =======   ==  ======  ======  ========
/dev/sd

以上是关于整理全网Shell脚本合集,Java脚本,运维脚本,告警脚本,监控脚本,日志脚本,docker脚本等---------持续更新!的主要内容,如果未能解决你的问题,请参考以下文章

日常巡检shell脚本

科普三大数据库运维脚本合集(建议收藏)

Linux下常用的shell脚本整理

linux系统安全审计脚本

免费领 | 《Shell脚本 100例》电子书免费拿,运维必备干货~

什么是Shell脚本?Shell脚本在Linux运维工作中的地位!