linux运维初级班shell脚本编程考试及参考答案20110523
############################################
#《老男孩linux就业培训中心-初级班第七期课前考试
#shell脚本编程实战模拟考试
#date:2011-5-23
#出题人:老男孩
#QQ:31333741 MAIL:[email protected]
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即将开放)
############################################
shell脚本编程实战模拟考试题(上机)
一、考试前准备工作:
1.执行yum install httpd -y 安装httpd。
2.检查安装情况
[root@oldboy-B tmp]# rpm -qa httpd
httpd-2.2.3-45.el5.centos.1
3.启动httpd,并检查
[root@oldboy-B tmp]# /etc/init.d/httpd start
启动 httpd:[确定]
[root@oldboy-B tmp]# netstat -lnt|grep 80 #==>检查端口
tcp 0 0 :::80 :::* LISTEN
[root@oldboy-B tmp]# ps -ef|grep httpd#==>检查进程
root 4041 1 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4043 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4044 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4045 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4046 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4047 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4048 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4049 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
apache 4050 4041 0 14:24 ? 00:00:00 /usr/sbin/httpd
root 4061 3793 0 14:25 pts/0 00:00:00 grep httpd
二、上机考试要求:
1.开发shell脚本/server/scripts/httpdctl,实现通过脚本httpdctl来控制apache服务的启动,停止,
重起。启动时命令要求:httpdctl start|stop|restart
提示:需要将httpdctl路径加到系统全局环境变量里。
参考答案:
[root@oldboy-B scripts]# pwd
/server/scripts
a.if语句实现:
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
if [ "$1" = "start" ];then
/etc/init.d/httpd $1 >/dev/null 2>&1
echo "启动 httpd:[确定]"
elif [ "$1" = "stop" ];then
/etc/init.d/httpd $1
elif [ "$1" = "restart" ];then
/etc/init.d/httpd $1 >/dev/null 2>&1
echo "重起 httpd: [确定]"
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
提示:这个问题用case语句实现效果更佳。 本文因学生还未讲解到case.
[root@oldboy-B scripts]# echo 'export PATH=/server/scripts:$PATH' >>/etc/profile
#===>将httpdctl路径加到系统全局环境变量里。
[root@oldboy-B scripts]# source /etc/profile
#===>使httpdctl路径加到系统全局环境变量生效
[root@oldboy-B scripts]# which httpdctl
/server/scripts/httpdctl
[root@oldboy-B scripts]# httpdctl stop
停止 httpd:[确定]
[root@oldboy-B scripts]# httpdctl start
启动 httpd:[确定]
[root@oldboy-B scripts]# httpdctl restart
重起 httpd: [确定]
-------------------------------------------------------
b.把相同的路径用变量替换[脚本改进]
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
HTTPDPATH=/etc/init.d/httpd
if [ "$1" = "start" ];then
$HTTPDPATH $1 >/dev/null 2>&1
echo "启动 httpd:[确定]"
elif [ "$1" = "stop" ];then
$HTTPDPATH $1
elif [ "$1" = "restart" ];then
$HTTPDPATH $1 >/dev/null 2>&1
echo "重起 httpd: [确定]"
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
-------------------------------------------------------
c.调用系统函数functions,实现更专业友好的提示[继续脚本改进]
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPDPATH=/etc/init.d/httpd
if [ "$1" = "start" ];then
$HTTPDPATH $1 >/dev/null 2>&1
action "启动 httpd:" /bin/true
elif [ "$1" = "stop" ];then
$HTTPDPATH $1
elif [ "$1" = "restart" ];then
$HTTPDPATH $1 >/dev/null 2>&1
action "重起 httpd:" /bin/true
else
echo "用法: $0 {start|stop|restart}"
exit 1
fi
提示:注意action "启动 httpd:" /bin/true的用法
[root@oldboy-B scripts]# httpdctl stop
停止 httpd:[确定]
[root@oldboy-B scripts]# httpdctl start
启动 httpd: [确定]
[root@oldboy-B scripts]# httpdctl restart
重起 httpd: [确定]
-------------------------------------------------------
d.case语句实现方式
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPDPATH=/etc/init.d/httpd
case "$1" in
start)
$HTTPDPATH $1 >/dev/null 2>&1
action "启动 httpd:" /bin/true
;;
stop)
$HTTPDPATH $1
;;
restart)
$HTTPDPATH $1 >/dev/null 2>&1
action "重起 httpd:" /bin/true
;;
*)
echo "用法: $0 {start|stop|restart}"
esac
e.更专业的实现方法
[root@oldboy-B scripts]# cat httpdctl
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
start() {
daemon httpd >/dev/null 2>&1
RETVAL=$?
action "启动 httpd:" /bin/true
return $RETVAL
}
stop() {
killproc httpd >/dev/null 2>&1
[ $? -eq 0 ] && action "停止 httpd:" /bin/true
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac
exit $RETVAL
f.系统自带的才是最好的。
请less /etc/init.d/httpd
要求:请所有同学用中文注释httpd脚本每一行,不会的可以咨询老师。
##############################################################
##############################################################
2.开发shell脚本apachemon,实现通过该脚本定时(每隔5分钟)监控http服务的运行状态,如果未运行则自动
调用httpdctl启动http服务,并发送mail通知系统管理员。
注意:监控方法可以为端口、进程、、URL模拟访问方式,或者三种方法综合。
说明:由于截止到目前仅讲了if语句,因此,就请大家用if语句来实现。
考试时间:15分钟
[root@oldboy-B scripts]# cat apachemon
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
if [ $HTTPPRONUM -lt 1 ];then
action "httpd is not running" /bin/false
action "httpd is not running" /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action "httpd is restart" /bin/true
mail -s "`uname -n`'s httpd restarted at `(date)`" [email protected] </tmp/httpd.log
exit 1
else
action "httpd is running" /bin/true
exit 0
fi
[root@oldboy-B scripts]# apachemon
httpd is running [确定]
[root@oldboy-B scripts]# pkill httpd
[root@oldboy-B scripts]# ps -ef|grep http|grep -v grep
[root@oldboy-B scripts]# apachemon
httpd is not running [失败]
httpd is restart [确定]
[root@oldboy-B scripts]# ps -ef|grep http|grep -v grep
root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
###脚本改进####
脚本改进测试:
[root@oldboy-B /]# echo oldboytest >/var/www/html/index.htm
[root@oldboy-B /]# wget --quiet --spider http://10.0.0.161/index.htm
[root@oldboy-B /]# echo $?
0
[root@oldboy-B /]# ll index.htm
ls: index.htm: 没有那个文件或目录
[root@oldboy-B scripts]# cat apachemon1
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
#HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` #=====>这个是基于http方式进行判断
wget --quiet --spider http://10.0.0.161/index.htm #=====>这个是基于WGET URL方式进行判断
if [ $? -ne 0 ];then
#action "httpd is not running" /bin/false #=====>正式运行,最好注释掉
action "httpd is not running" /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action "httpd is restart" /bin/true >>/tmp/httpd.log
mail -s "`uname -n`'s httpd restarted at `(date)`" [email protected] </tmp/httpd.log
exit 1
else
action "httpd is running" /bin/true
exit 0
fi
真正使用时,有些输出是不需要的就去掉
[root@oldboy-B scripts]# cat apachemon1
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
wget --quiet --spider http://10.0.0.161/index.htm #=====>这个是基于WGET URL方式进行判断
if [ $? -ne 0 ];then
action "httpd is not running" /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action "httpd is restart" /bin/true >>/tmp/httpd.log
mail -s "`uname -n`'s httpd restarted at `(date)`" [email protected] </tmp/httpd.log
exit 1
fi
提示:请同学们详细总结wget命令,做为下周作业提交。
多条件判断的写法
[root@oldboy-B scripts]# cat apachemon1
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /etc/init.d/functions
HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`
HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
wget --quiet --spider http://10.0.0.161/index.htm && RETVAL=$?
if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then
#if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \< "1" ] ;then
#提示:以上是两个if为多条件判断的写法,建议同学们掌握第一个。
action "httpd is not running" /bin/false
action "httpd is not running" /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action "httpd is restart" /bin/true
mail -s "`uname -n`'s httpd restarted at `(date)`" [email protected] </tmp/httpd.log
exit 1
else
action "httpd is running" /bin/true
exit 0
fi
以上为 老男孩 给的课前考试的参考答案,欢迎大家讨论、交流,共同提高进步。
后记:shell编程能力 是衡量linux运维工程师水平的一个重要砝码,因此,必须要掌握它。。