shell脚本检测mysql状态
方法一:
if [`netstat -tunlp|grep mysq1 | WC -1` ="1"] then echo "MySQL is running else echo "MySQL is stopped." systemctl start mariadb fi
方法二:
if [`ss -tunlp|grep mysq1 | WC -1` ="1"] then echo "MySQL is running else echo "MySQL is stopped." systemctl start mariadb fi
方法三:
if [`losf -i tcp:3380| WC -1` ="1"] then echo "MySQL is running else echo "MySQL is stopped." systemctl start mari adb fi
方法四:
执行一个文件(php/java检测mysql连接状态),根据命令的返回值,判断mysql是否存活
if ["$?" -eq 0 ]
shell脚本检测内存,CPU,内存状态
#!/bin/bash
now=`date -u -d"+8 hour" +'%Y-%m-%d %H:%M:%S'`
#cpu使用阈值
cpu_warn='80'
#mem空闲阈值
mem_warn='80'
#disk使用阈值
disk_warn='90'
#---cpu
item_cpu () {
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}'|cut -f 1 -d "."`
cpu_use=`expr 100 - $cpu_idle`
echo "$now 当前cpu使用率为 $cpu_use" >> /opt/cpu.log
if [ $cpu_use -gt $cpu_warn ]
then
echo "cpu warning!!!"
else
echo "cpu ok!!!"
fi
}
#---mem
item_mem () {
#MB为单位
mem_free=`free -m | grep "Mem" | awk '{print $4+$6}'`
echo "$now 当前内存剩余空间为 ${mem_free}MB" >> /opt/mem.log
if [ $mem_free -lt $mem_warn ]
then
echo "mem warning!!!"
else
echo "mem ok!!!"
fi
}
#---disk
item_disk () {
disk_use=`df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"`
echo "$now 当前磁盘使用率为 $disk_use" >> /opt/disk.log
if [ $disk_use -gt $disk_warn ]
then
echo "disk warning!!!"
else
echo "disk ok!!!"
fi
}
item_cpu
item_mem
item_disk
一键部署 LNMP
#!/bin/bash
yum ‐y install httpd
yum ‐y install mariadb mariadb‐devel mariadb‐server
yum ‐y install php php‐mysql
systemctl start httpd mariadb
systemctl enable httpd mariadb
测试网段主机存活状态
#!/bin/bash
for i in {1..254}
do
ping ‐c2 ‐i0.3 ‐W1 192.168.4.$i &>/dev/null
if [ $? –eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
done