企业实践题16:
企业案例:写网络服务独立进程模式下rsync的系统启动脚本
例如:/etc/init.d/rsyncd{start|stop|restart} 。
要求:
1.要使用系统函数库技巧。
2.要用函数,不能一坨SHI的方式。
3.可被chkconfig管理。
脚本1:自己写的
#!/bin/bash
pidfile="/var/run/rsyncd.pid"
result=`ps aux|grep 'rsync --daemon --config=*'|grep -v grep`
Rstatus(){
if [ "$result"X = X ];then
echo "rsyncd is not running !"
else
echo "rsyncd is running"
fi
}
Rstart(){
if [ "$result"X == X ];then
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
echo "rsync service start.......OK"
else
echo "rsyncd is running"
fi
}
Rstop(){
if [ "$result"X == X ];then
echo "rsync service is down !"
exit 1
else
kill -9 $(cat $pidfile)
Rstatus2=`ps aux|grep 'rsync --daemon --config=*'|grep -v grep`
if [ "$Rstatus2"X = X ];then
rm -f $pidfile
echo "rsync service stop.......OK"
fi
fi
}
Rrestart(){
if [ "$result"X != X ];then
Rstop
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
echo "rsync service start.......OK"
else
echo "rsync service is down !"
fi
}
case $1 in
"start" )
Rstart
;;
"stop" )
Rstop
;;
"restart" )
Rrestart
;;
"status" )
Rstatus
;;
* )
echo "argument is start|stop|status|restart"
;;
esac
脚本2:网上找的
#!/bin/bash #this script for start|stop rsync daemon service status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep') pidfile="/var/run/rsyncd.pid" start_rsync="rsync --daemon --config=/etc/rsyncd/rsyncd.conf" #start rsync function rsyncstart() { if [ "${status1}X" == "X" ];then rm -f $pidfile ${start_rsync} status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep') if [ "${status2}X" != "X" ];then echo "rsync service start.......OK" fi else echo "rsync service is running !" fi } #stop rsync function rsyncstop() { if [ "${status1}X" != "X" ];then kill -9 $(cat $pidfile) #读取并结束 进程 pid号 status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep') if [ "${statusw2}X" == "X" ];then echo "rsync service stop.......OK" fi else echo "rsync service is not running !" fi } #status function rsyncstatus() { if [ "${status1}X" != "X" ];then echo "rsync service is running !" else echo "rsync service is not running !" fi } #restart function rsyncrestart() { if [ "${status1}X" == "X" ];then echo "rsync service is not running..." rsyncstart else rsyncstop rsyncstart fi } case $1 in "start") rsyncstart ;; "stop") rsyncstop ;; "status") rsyncstatus ;; "restart") rsyncrestart ;; *) echo echo "Usage: $0 start|stop|restart|status" echo esac