①通过端口:
#!/bin/sh
. /etc/init.d/functions
port=`lsof -i:80|grep nginx|wc -l`
if [ $port -ge 2 ];then
action "Nginx is running" /bin/true
else
action "Nginx is not running" /bin/false
/application/nginx/sbin/nginx
action "starting Nginx" /bin/true
fi
②通过进程:
#!/bin/sh
. /etc/init.d/functions
port=`ps -ef|grep nginx|grep -v grep|wc -l`
if [ $port -ge 2 ];then
action "Nginx is running" /bin/true
else
action "Nginx is not running" /bin/false
/application/nginx/sbin/nginx
action "starting Nginx" /bin/true
fi
③wget返回内容:
#!/bin/sh
. /etc/init.d/functions
port=`wget -T 5 --spider http://172.19.5.8 &>/dev/null`
if [ $? -eq 0 ];then
action "Nginx is running" /bin/true
else
/application/nginx/sbin/nginx
action "starting Nginx" /bin/true
fi
④curl返回值200:
#!/bin/sh
. /etc/init.d/functions
port=`curl -s -I -w "%{http_code}\n" 172.19.5.8 -o /dev/null`
if [ "$port" == "200" ];then
action "Nginx is running" /bin/true
else
/application/nginx/sbin/nginx
action "starting Nginx" /bin/true
fi
监控Memcached缓存服务是否正常,模拟用户(web客户端)检测。
使用nc命令加上set/get来模拟检测。
面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次。
#!/bin/bash
. /etc/init.d/functions
port=`netstat -lntup|grep 873|wc -l`
if [ ! -f /etc/rsyncd.conf ]
then
action "rsync配置" /bin/false
exit 1
elif [ ! -f /usr/bin/rsync ]
then
action "rsync命令" /bin/false
exit
fi
case "$1" in
start)
if [ $port -lt 2 ];then
rsync --daemon
action "Starting rsync..." /bin/true
else
action "Rsync is running..." /bin/true
fi
;;
stop)
if [ $port -ge 2 ];then
killproc rsync
action "Stopping rsync..." /bin/true
else
action "Rsync is not running..." /bin/true
fi
;;
restart)
killproc rsync
rsync --daemon
action "Rsync is restarting." /bin/true
;;
*)
echo "USAGE:$0 {start|stop|restart}"
esac
开发MySQL多实例启动脚本:
已知MySQL多实例启动命令为:mysqld_safe --defaults-file=/data/3306/my.cnf&
停止命令为:mysqladmin -u root -poldboy123 -S/data/3306/mysql.sock shutdown
请完成mysql多实例启动启动脚本的编写
要求:用函数,case语句、if语句等实现。
#!/bin/bash
. /etc/init.d/functions
Port="3306"
Myuser="root"
Mypass="123456"