对于if 语法 我们不过多做介绍,这里直接上实例,以开发rsync服务启动脚本为例,先对rsync做个简单介绍
[root@backup ~]# rpm -qa|grep rsync rsync-3.0.6-12.el6.x86_64 [root@backup ~]# [root@backup ~]# cat /etc/rsync rsyncd.conf rsync.password [root@backup ~]# cat /etc/rsyncd.conf #Rsync server uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.80.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password ########### [backup] comment = backup by martin 20170613 path = /backup [root@backup ~]# cat /etc/rsync rsyncd.conf rsync.password [root@backup ~]# cat /etc/rsync.password rsync_backup:123456 [root@backup ~]# [root@backup ~]# rsync --daemon #启动rsync [root@backup ~]# netstat -lntup|grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 24837/rsync tcp 0 0 :::873 :::* LISTEN 24837/rsync
[root@backup ~]# pkill rsync #停止rsync
[root@backup ~]# netstat -lntup|grep rsync
判断rsync服务是否启动的方法
常规方法有检测端口以及进程是否存在,可以当服务启动时,创建一个锁文件 /var/lock/subsys/ 而当服务停止时,就删除这个锁文件,这样就可以通过判断这个文件有无来确定服务是否是启动的状态,这是一些系统脚本常用的手法,脚本开发如下
[root@backup ~]# cat rsyncd.sh #!/bin/bash # chkconfig: 2345 20 80
# 2345是linux的启动运行级别,20表示开始启动顺序,80表示脚本停止顺序,这两个数字都是不超过99的数字,应用服务一般要考后启动为佳,越早停止越好
if [ $# -ne 1 ];then echo $"usage: $0 {start|stop|restart}" exit 1 fi if [ "$1" = "start" ];then rsync --daemon sleep 2 if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then echo "rsyncd is started." fi elif [ "$1" = "stop" ];then pkill rsync &>/dev/null sleep 2 if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then echo "rsyncd is stopped." fi elif [ "$1" = "restart" ];then pkill rsync sleep 2 killpro=`netstat -lntup|grep rsync|wc -l` rsync --daemon sleep 1 startpro=`netstat -lntup|grep rsync|wc -l` if [ $killpro -eq 0 -a $startpro -ge 1 ];then echo "rsyncd is restarted." fi else echo $"usage: $0 {start|stop|restart}" exit 1 fi [root@backup ~]# [root@backup ~]# [root@backup ~]# ps -ef|grep rsync root 24892 24780 0 00:00 pts/0 00:00:00 grep rsync [root@backup ~]# [root@backup ~]# [root@backup ~]# sh rsyncd.sh start rsyncd is started. [root@backup ~]# [root@backup ~]# ps -ef|grep rsync root 24895 1 0 00:00 ? 00:00:00 rsync --daemon root 24902 24780 0 00:00 pts/0 00:00:00 grep rsync [root@backup ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 24895/rsync tcp 0 0 :::873 :::* LISTEN 24895/rsync [root@backup ~]# [root@backup ~]# sh rsyncd.sh restart rsyncd is restarted. [root@backup ~]# netstat -lntup |grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 24928/rsync tcp 0 0 :::873 :::* LISTEN 24928/rsync [root@backup ~]# sh rsyncd.sh stop rsyncd is stopped. [root@backup ~]# netstat -lntup |grep rsync
使用shell函数封装的方法来优化上面的脚本,改良后的脚本如下
[root@backup ~]# cat /etc/init.d/rsyncd #!/bin/sh # chkconfig: 2345 20 80 . /etc/init.d/functions if [ $# -ne 1 ];then echo $"USAGE: $0 {start|stop|restart}" exit 1 fi start(){ rsync --daemon sleep 1 if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then action "rsyncd is started." /bin/true else action "rsyncd is stopped." /bin/false fi } stop(){ killall rsync &>/dev/null sleep 2 if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then action "rsyncd is stopped." /bin/true else action "rsyncd is started." /bin/false fi } restart(){ stop sleep 1 start } case "$1" in start) start ;; stop) stop ;; restart) restart ;; *) echo "echo "USAGE: $0 {start|stop|restart}"" esac