编写一个rsync服务启停的shell脚本

前言

         利用linux的查询服务端口用于判断编写条件触发服务的启动、停止以及更新


一、Shell

        Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

二、编写rsync服务启停脚本

  • 根据/usr/lib/systemd/system/rsyncd.service查看服务启动命令
[root@controller init.d]# cat /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"  #启动方法

利用netstat -tunlp |grep rsync |wc -l 命令查看服务是否开启,方便脚本中进行条件判断

[root@controller init.d]# cat rsync 
#!/bin/bash
status=`netstat -tunlp |grep rsync |wc -l`

if [ "$1" = "start" ]
  then
    /usr/bin/rsync --daemon
  if [ "$status" -lt 1 ]
    then 
      sleep 2
      echo "Rsync started !!"
  else
    echo "Rsync already Started!"
  fi
elif [ "$1" = "stop" ]
  then 
    if [ "$status" -gt 0 ]
      then
        killall rsync
        sleep 2
        echo "Rsync stoped !!"
    else
      echo "Get something wrong,please check the status"
    fi
elif [ "$1" = "restart" ]
  then
    killall rsync
    echo "Rsync stopping!!"
    sleep 2
    echo "Rsync stoped !!"
    if [ "$status" -lt 1 ]
      then
        /usr/bin/rsync --daemon
        echo "Rsync starting!!"
    else
      echo "Rsync already Started!"
    fi
    /usr/bin/rsync --daemon
else
  echo "Usage ./rsync {start|stop|restart}"
fi

三、运行测试

[root@controller init.d]# ./rsync.sh 
Usage ./rsync {start|stop|restart}
[root@controller init.d]# netstat -ntpl |grep rsync
[root@controller init.d]# ./rsync.sh start
Rsync started !!
[root@controller init.d]# netstat -ntpl |grep rsync
tcp        0      0 127.0.0.1:873           0.0.0.0:*               LISTEN      8099/rsync          
[root@controller init.d]# ./rsync.sh stop
Rsync stoped !!
[root@controller init.d]# netstat -ntpl |grep rsync
[root@controller init.d]# ./rsync.sh restart
Rsync stopping!!
Rsync stoped !!
Rsync starting!!
[root@controller init.d]# netstat -ntpl |grep rsync
tcp        0      0 127.0.0.1:873           0.0.0.0:*               LISTEN      8161/rsync   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值