1. 程式人生 > >rsync啟動腳本編寫

rsync啟動腳本編寫

rsync服務啟動腳本 service start

需求:寫一個rsync服務啟動腳本

思路:

1、首先對腳本參數個數進行判斷如果傳參個數不等於1,則echo "Usage: $0 {start|restart|stop}"

2、定義函數service,通過case進行對腳本傳參值的流程控制判斷

3、啟動服務采用命令rsync --daemon

4、停止服務采用kill -9 進程和刪除rsync的PID文件


[[email protected] 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


本文出自 “常想一二” 博客,請務必保留此出處http://250919938.blog.51cto.com/962010/1923970

rsync啟動腳本編寫