rsync启动脚本编写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsync启动脚本编写相关的知识,希望对你有一定的参考价值。
需求:写一个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启动脚本编写的主要内容,如果未能解决你的问题,请参考以下文章