rsync启停脚本开发
检查是否有rsync
[root@localhost ~]# rpm -qa rsync
rsync-3.1.2-12.el7_9.x86_64
启动服务
/usr/bin/rsync --daemon
检查服务是否启动
[root@localhost ~]# ps -ef |grep rsync |grep -v grep
root 12182 1 0 14:37 ? 00:00:00 /usr/bin/rsync --daemon
[root@localhost ~]# netstat -tunlp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12182/rsync
tcp6 0 0 :::873 :::* LISTEN 12182/rsync
停止rsync服务
[root@localhost ~]# pkill rsync
开发启停脚本
/etc/init.d/rsync
#!/bin/bash
# author:sprr
# -ge if条件的大于等于 -ne bu等于 -gt 大于,$# 返回传递给脚本参数个数,$0 取得脚本文件名
# 限制用户必须传入一个参数
if [ "$#" -ne 1 ]
then
echo "Usage:$0 {start|stop|restart}"
exit 1
fi
#检测rsync是否启动
#function check(){
#rsync_status=`netstat -tunlp|grep rsync|wc -l`
#return rsync_status
#}
if [ "$1" = "start" ]
then
rsync_status=`netstat -tunlp|grep rsync|wc -l`
if [ "$rsync_status" -gt 0 ]
then
echo "rsync already started!"
exit 1
else
/usr/bin/rsync --daemon
exit 0
fi
elif [ "$1" = "stop" ]
then
killall rsync &>/dev/null
sleep 2
rsync_status=`netstat -tunlp|grep rsync|wc -l`
if [ "$rsync_status" -eq 0 ]
then
echo "rsync is stopped!"
exit 0
fi
elif [ "$1" = "restart" ]
then
killall rsync &>/dev/null
/usr/bin/rsync --daemon
sleep 2
rsync_status=`netstat -tunlp|grep rsync|wc -l`
if [ "$rsync_status" -gt 0 ]
then
echo "rsync is restarted!"
fi
else
echo "Usage:$0 {start|stop|restart}"
fi