[root@backup ~]# vim /etc/init.d/rsync
#!/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"
#!/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
}
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
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
(2)给脚本设置可执行权限:
[root@backup ~]# chmod a+x /etc/init.d/rsync
(3)启动与重启:
启动命令: [root@backup ~]# service rsync start
重启命令: [root@backup ~]# service rsync restart
或者可以停止后再次启动:
[root@backup ~]# service rsync stop
[root@backup ~]# service rsync start
启动命令: [root@backup ~]# service rsync start
重启命令: [root@backup ~]# service rsync restart
或者可以停止后再次启动:
[root@backup ~]# service rsync stop
[root@backup ~]# service rsync start
总结:
1.status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
if [ "${status1}X" == "X" ];then
这两句用来测试命令行有没有参数的
2. kill -9 $(cat $pidfile)
#读取并结束 进程 pid号
#读取并结束 进程 pid号