需求:写一个rsync服务启动脚本

思路:

1、首先对脚本参数个数进行判断如果传参个数不等于1,则echo "Usage: $0 {start|restart|stop}"

2、定义函数service,通过case进行对脚本传参值的流程控制判断

3、启动服务采用命令rsync --daemon

4、停止服务采用kill -9 进程和删除rsync的PID文件


[root@salt-master test]# cat /etc/init.d/rsyncd 

#!/bin/bash

#rsyncd启动脚本

#

. /etc/init.d/functions

PID=`ps -ef|grep "rsync --daemon" |egrep -v grep |awk '{print $2}'`

pidfile=/var/run/rsyncd.pid

if [ $# -ne 1 ];then

echo "Usage: $0 {start|restart|stop}"

exit 5

fi

result=$1

service() {

case $result in

start)

[ -n "$PID" ] && {

action "rsyncd up..." /bin/false

exit 9

}

rsync --daemon &>/dev/null

[ $? -eq 0 ] && action "rsyncd up..." /bin/true || action "rsyncd" /bin/false

;;

stop)

[ -z "$PID" ] && {

action "rsyncd down..." /bin/false

exit 7 

}

[ -n "$PID" ] && kill -9 $PID || action "rsyncd..." /bin/false

[ -f $pidfile ] && {

rm -rf $pidfile

action "rsyncd down..." /bin/true

} || action "rsyncd down..." /bin/false

;;

restart)

#[ -z "$PID" ] && {

# action "rsyncd down..." /bin/false

# }

#[ -n "$PID" ] && kill -9 $PID || action "rsyncd down..." /bin/false

#[ -f $pidfile ] && {

#rm -rf $pidfile

#action "rsyncd down..." /bin/true

#} || action "rsyncd down..." /bin/false

if [ -z "$PID" ];then

action "rsyncd down..." /bin/false

elif [ -n "$PID" ];then

kill -9 $PID

[ -f $pidfile ] && {

rm -rf $pidfile

action "rsyncd down..." /bin/true

}

  else

action "rsyncd down..." /bin/false

fi

sleep 1

rsync --daemon &>/dev/null

[ $? -eq 0 ] && action "rsyncd up..." /bin/true || action "rsyncd up..." /bin/false

;;

*)

echo "Usage: $0 {start|restart|stop}"

;;

esac

}

service