centos6、centos7 脚本管理rsync服务状态

1.环境准备

     centos6  --------- 虚拟机

     centos7  --------- 虚拟机

2. centos6 编写脚本

  • 分析脚本管理的逻辑
  1. centos6中管理 服务的脚本都是在 /etc/init.d/目录下的,我们要在此目录下创建一个管理的脚本,
  2. centos系统是通过什么命令来实现启动服务的?      命令:--daemon
  3. 服务启动了怎样关闭?    通过杀死该服务进程来实现。
  4. 服务进程的pid是在服务启动后在指定的文件中产生一个文件,里面就是该服务的pid
  • 实际命令
  1. 启动服务:rsync --daemon
  2. 查看服务进程:ps -ef|grep rsync
  3. 杀死进程:kill 进程pid
  • 脚本内容
#!/bin/bash

pid_file=/var/run/rsyncd.pid

choice=$1

start() {
     [ -s "$pid_file" ] && {
        echo "rsync has runing"
        # exit;
     } || {
        rsync --daemon
     }
}

stop() {
       [ -s "$pid_file" ] && {
          kill `cat $pid_file` 
       } ||{
          echo "raync has stoped"
       }
}

restart() {
       [ -s "$pid_file" ] && {
           kill `cat $pid_file`
           sleep 2
           rsync --daemon
       } || {
           rsync --daemon
       }
}
main() {
if [ "$choice" = "start" ]
 then
      start 
 elif [ "$choice" = "stop" ]
    then
      stop
 elif [ "$choice" = "restart" ]
    then
      restart
 else
    echo "Usage: $0 start|stop|restart"
fi;}

main
  • 测试
  1. 命令:/etc/init.d/rsync start|stop|restart
  • 注意
  1. 脚本执行的时, 脚本一定要在/etc/init.d/目录下,其自身一定要有执行权限
  2. 命令:chmod +x 脚本名称

3.centos6 通过chkconfig管理服务开机自启动

  • 脚本开头必须要有chkconfig要求的格式
  • 第一步:在脚本加入正确格式
# chkconfig: 2345 99 98

命令解释
chkconfig: 默认在2345系统级别 开机自启动 开机顺序 关机顺序
  • 第二步:通过chkconfig命令将其让chkconfig管理
chkconfig --add rsyncd
  • 第三步:查看是否被管理
#通过过滤查看,如下图,则管理成功
chkconfig |grep rsync

  • 开机自启和不自启设置
#开机自启
chkconfig iptables on

#开机不自启
chkconfig iptables off

 

4.centos7 实现脚本管理服务,并通过systemctl管理

  • 将centos6写好的脚本拷贝到centos7中的/server/scripts/目录下

   centos7管理需要配置systemctl的配置文件

 

  • 编写systemctl配置文件路径:/usr/lib/systemd/system/服务的名称.service
  • 配置文件内容
[Unit]
After=network.target

[Service]
Type=forking
ExecStart=/server/scripts/rsyncd start
ExecReload=/server/scripts/rsyncd restart
ExecStop=/server/scripts/rsyncd stop

[Install]
WantedBy=multi-user.target

 

  • 测试

    systemctl start rsync。service

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值