Example one: Web server Nginx Script service monitoring
[Email protected]/server/scripts] #cat jiance_ng.sh
#!/bin/bash
if! Ss-lntup|grep [N]ginx &>/dev/null
Then
/etc/init.d/nginx start
echo "Nginx dead,but It's OK" |mail-s "WHW11" [email protected]
Fi
http_num=$ (curl-i www.oldboyedu.com-s-W%{http_code}-o/dev/null)
If [${http_num}-ne];then
/etc/init.d/nginx start
echo "Nginx dead,but It's OK" |mail-s "WHW11" [email protected]
Fi
Example two: MySQL Server service monitoring
[email protected] scripts]# cat jiance_db.sh
#!/bin/bash
if! Ps-ef|grep [M]ysqld &>/dev/null
Then
Systemctl Start Mariadb.service &>/dev/null
echo "MySQL dead,but it is ok" |mail-s "whw111" [email protected]
Fi
Example three: Memcache Cache server Monitoring
- [email protected] scripts]# cat jiance_mem2.sh****
#!/bin/bash
port_status=$ (Ss-lntup|grep 11211|wc-l)
key1= "oldboy0987"
printf "Set key011 0 0 10\r\n${key1}\r\n" |nc 10.0.0.21 11211 &>/dev/null
key2=$ (printf "Get key011\r\n" |nc 10.0.0.21 11211|sed-n ' 2p ' |tr-d ' \ R ')
If [$port _status-eq 0];then
Systemctl Stop Memcached.service
Systemctl Start Memcached.service
if! [${key1} = = ${key2}];then
Systemctl Restart Memcached.service
echo "Memcache dead,but It's OK" |mail-s "WHW11" [email protected]
Fi
Fi
Example four: Print a selection menu and install a different Web service by selecting one button * * *
[Email protected]/server/scripts] #cat yijian.sh
#!/bin/bash
Cat <<end
1.[install Lamp]
2.[install LNMP]
3.exit
END
Read-p "Please enter options:" num
. /etc/init.d/functions
Case $num in
1)
if [-x/server/scripts/lamp.sh];then
/server/scripts/lamp.sh
Action "Start installing lamp"/bin/true
Action "lamp is Installed"/bin/true
Else
echo "One-click script does not exist or no execute permission"
Fi
;;
2)
if [-x/server/scripts/lnmp.sh];then
/server/scripts/lnmp.sh
Action "Start installing LNMP"/bin/true
Action "LNMP is installed"/bin/true
Else
echo "One-click script does not exist or no execute permission"
Fi
;;
3)
Exit 3
;;
*)
echo "Your input is wrong"
Esac
Example five: rsync start stop script
[Email protected]/server/scripts] #cat rsync_on.sh
#!/bin/bash
if! [-f/etc/rsyncd.conf];then
echo "Please create rsync profile First"
Exit 2
Fi
Read-p "Please enter the action to be performed:" cmd
. /etc/init.d/functions
Case $cmd in
Start
If Ss-lntup|grep rsync &>/dev/null;then
echo "Rsync service Started"
Exit 2
Else
Rsync--daemon
Action "rsync start"/bin/true
Fi
;;
Stop
If Ss-lntup|grep rsync &>/dev/null;then
Killall rsync
Sleep 3
Action "Rsync Stop"/bin/true
Else
echo "Rsync is a stop state"
Fi
;;
Restart
If Ss-lntup|grep rsync &>/dev/null;then
Pkill-f rsync
Rsync--daemon
Action "Rsync Stop"/bin/true
Action "rsync start"/bin/true
Else
Rsync--daemon
Action "Rsync Stop"/bin/false
Action "rsync start"/bin/true
Fi
;;
*)
echo "Input instruction Error: (Start|stop|restart)"
Esac
Shell's service anomaly monitoring case