rsync随机启动脚本

2023-11-17

服务端

 

 1 #!/bin/sh
 2 # chkconfig: 2345 21 60
 3 # description: Saves and restores system entropy pool for \
 4 #create by xiaohu
 5 #2014.06.02
 6 #This script is the Rsync service script
 7 . /etc/init.d/functions
 8 case "$1" in
 9   start)
10         echo "rsync is starting"
11         /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf
12         sleep 2
13         myport=`netstat -lnt|grep 873|wc -l`
14         if [ $myport -eq 2 ]
15         then
16         action "rsync start"   /bin/true
17         else
18         action "rsync start"   /bin/false
19         fi
20         ;;
21   stop)
22         echo "rsync is stoping"
23         myport=`netstat -lnt|grep 873|wc -l`
24         if [ $myport -eq 2 ]
25         then 
26         killall rsync &>/dev/null
27         sleep 2
28         killall rsync &>/dev/null
29         sleep 1
30         fi
31         myport=`netstat -lnt|grep 873|wc -l`
32         if [ $myport -ne 2 ]
33         then
34         action "rsync stop"   /bin/true
35         else
36         action "rsync stop"   /bin/false
37         fi
38         ;;
39   restart)
40         if [ `netstat -lnt|grep 873|wc -l` -eq 0 ]
41         then
42         /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf
43         sleep 2
44         myport=`netstat -lnt|grep 873|wc -l`
45         if [ $myport -eq 2 ]
46         then
47         action "rsync restart"   /bin/true
48         else
49         action "rsync restart"   /bin/false
50         exit
51         fi
52         else
53         killall rsync &>/dev/null
54         sleep 2
55         killall rsync &>/dev/null
56         sleep 1
57         /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf
58         sleep 2
59         myport=`netstat -lnt|grep 873|wc -l`
60         if [ $myport -eq 2 ]
61         then
62         action "rsync restart"   /bin/true
63         else
64         action "rsync restart"   /bin/false
65         fi
66         fi
67         ;;
68   status)
69         myport=`netstat -lnt|grep 873|wc -l`
70         if [ $myport -eq 2 ]
71         then
72         echo  "rsync is running"
73         else
74         echo "rsync is stoped"
75         fi
76         ;;
77   *)
78         echo $"Usage: $0 {start|stop|status|restart}"
79         ;;
80 esac
View Code

 

 

客户端

 

  1 #! /bin/sh
  2 
  3 ### BEGIN INIT INFO
  4 # Provides:          rsyncd
  5 # Required-Start:    $remote_fs $syslog
  6 # Required-Stop:     $remote_fs $syslog
  7 # Should-Start:      $named autofs
  8 # Default-Start:     2 3 4 5
  9 # Default-Stop:      
 10 # Short-Description: fast remote file copy program daemon
 11 # Description:       rsync is a program that allows files to be copied to and
 12 #                    from remote machines in much the same way as rcp.
 13 #                    This provides rsyncd daemon functionality.
 14 ### END INIT INFO
 15 
 16 set -e
 17 
 18 # /etc/init.d/rsync: start and stop the rsync daemon
 19 
 20 DAEMON=/usr/bin/rsync
 21 RSYNC_ENABLE=false
 22 RSYNC_OPTS=''
 23 RSYNC_DEFAULTS_FILE=/etc/default/rsync
 24 RSYNC_CONFIG_FILE=/etc/rsyncd.conf
 25 RSYNC_PID_FILE=/var/run/rsync.pid
 26 RSYNC_NICE_PARM=''
 27 RSYNC_IONICE_PARM=''
 28 
 29 test -x $DAEMON || exit 0
 30 
 31 . /lib/lsb/init-functions
 32 
 33 if [ -s $RSYNC_DEFAULTS_FILE ]; then
 34     . $RSYNC_DEFAULTS_FILE
 35     case "x$RSYNC_ENABLE" in
 36     xtrue|xfalse)    ;;
 37     xinetd)        exit 0
 38             ;;
 39     *)        log_failure_msg "Value of RSYNC_ENABLE in $RSYNC_DEFAULTS_FILE must be either 'true' or 'false';"
 40             log_failure_msg "not starting rsync daemon."
 41             exit 1
 42             ;;
 43     esac
 44     case "x$RSYNC_NICE" in
 45     x[0-9]|x1[0-9])    RSYNC_NICE_PARM="--nicelevel $RSYNC_NICE";;
 46     x)        ;;
 47     *)        log_warning_msg "Value of RSYNC_NICE in $RSYNC_DEFAULTS_FILE must be a value between 0 and 19 (inclusive);"
 48             log_warning_msg "ignoring RSYNC_NICE now."
 49             ;;
 50     esac
 51     case "x$RSYNC_IONICE" in
 52     x-c[123]*)    RSYNC_IONICE_PARM="$RSYNC_IONICE";;
 53     x)        ;;
 54     *)        log_warning_msg "Value of RSYNC_IONICE in $RSYNC_DEFAULTS_FILE must be -c1, -c2 or -c3;"
 55             log_warning_msg "ignoring RSYNC_IONICE now."
 56             ;;
 57     esac
 58 fi
 59 
 60 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 61 
 62 rsync_start() {
 63     if [ ! -s "$RSYNC_CONFIG_FILE" ]; then
 64         log_failure_msg "missing or empty config file $RSYNC_CONFIG_FILE"
 65         log_end_msg 1
 66         exit 0
 67     fi
 68     # See ionice(1)
 69     if [ -n "$RSYNC_IONICE_PARM" ] && [ -x /usr/bin/ionice ] &&
 70         /usr/bin/ionice "$RSYNC_IONICE_PARM" true 2>/dev/null; then
 71         /usr/bin/ionice "$RSYNC_IONICE_PARM" -p$$ > /dev/null 2>&1
 72     fi
 73     if start-stop-daemon --start --quiet --background \
 74         --pidfile $RSYNC_PID_FILE --make-pidfile \
 75         $RSYNC_NICE_PARM --exec $DAEMON \
 76         -- --no-detach --daemon --config "$RSYNC_CONFIG_FILE" $RSYNC_OPTS
 77     then
 78         rc=0
 79         sleep 1
 80         if ! kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
 81             log_failure_msg "rsync daemon failed to start"
 82             rc=1
 83         fi
 84     else
 85         rc=1
 86     fi
 87     if [ $rc -eq 0 ]; then
 88         log_end_msg 0
 89     else
 90         log_end_msg 1
 91         rm -f $RSYNC_PID_FILE
 92     fi
 93 } # rsync_start
 94 
 95 
 96 case "$1" in
 97   start)
 98     if "$RSYNC_ENABLE"; then
 99         log_daemon_msg "Starting rsync daemon" "rsync"
100         if [ -s $RSYNC_PID_FILE ] && kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
101         log_progress_msg "apparently already running"
102         log_end_msg 0
103         exit 0
104         fi
105             rsync_start
106         else
107             if [ -s "$RSYNC_CONFIG_FILE" ]; then
108                 [ "$VERBOSE" != no ] && log_warning_msg "rsync daemon not enabled in $RSYNC_DEFAULTS_FILE, not starting..."
109             fi
110     fi
111     ;;
112   stop)
113     log_daemon_msg "Stopping rsync daemon" "rsync"
114     start-stop-daemon --stop --quiet --oknodo --pidfile $RSYNC_PID_FILE
115     log_end_msg $?
116     rm -f $RSYNC_PID_FILE
117     ;;
118 
119   reload|force-reload)
120     log_warning_msg "Reloading rsync daemon: not needed, as the daemon"
121     log_warning_msg "re-reads the config file whenever a client connects."
122     ;;
123 
124   restart)
125     set +e
126     if $RSYNC_ENABLE; then
127         log_daemon_msg "Restarting rsync daemon" "rsync"
128         if [ -s $RSYNC_PID_FILE ] && kill -0 $(cat $RSYNC_PID_FILE) >/dev/null 2>&1; then
129         start-stop-daemon --stop --quiet --oknodo --pidfile $RSYNC_PID_FILE || true
130         sleep 1
131         else
132         log_warning_msg "rsync daemon not running, attempting to start."
133             rm -f $RSYNC_PID_FILE
134         fi
135             rsync_start
136         else
137             if [ -s "$RSYNC_CONFIG_FILE" ]; then
138                 [ "$VERBOSE" != no ] && log_warning_msg "rsync daemon not enabled in $RSYNC_DEFAULTS_FILE, not starting..."
139             fi
140     fi
141     ;;
142 
143   status)
144     status_of_proc -p $RSYNC_PID_FILE "$DAEMON" rsync
145     exit $?    # notreached due to set -e
146     ;;
147   *)
148     echo "Usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart|status}"
149     exit 1
150 esac
151 
152 exit 0
View Code

 

 


开机自动启动rsync

1. 扔脚本进去/etc/init.d/


2. 授权
chmod +x rsync


3. 一旦抛出:binsh^M错误就执行编码改写
设置dos统一编码
(请看rsync脚本抛出binsh^M bad interpreter文档)


4. 添加到服务
chkconfig --add ningx


5. 随机启动脚本带动rsync开机启动
chkconfig --level 2345 rsync on

 

 


 

执行脚本时发现如下错误:
/bin/sh^M: bad interpreter: 没有那个文件或目录

错误分析:
因为操作系统是windows,我在windows下编辑的脚本,所以有可能有不可见字符。
脚本文件是DOS格式的, 即每一行的行尾以\n\r来标识, 其ASCII码分别是0x0D, 0x0A.

可以有很多种办法看这个文件是DOS格式的还是UNIX格式的, 还是MAC格式的

解决方法:
vim filename
然后用命令
:set ff? #可以看到dos或unix的字样. 如果的确是dos格式的。


然后用
:set ff=unix #把它强制为unix格式的, 然后存盘退出。
再次运行脚本。

 

转载于:https://www.cnblogs.com/chenglee/p/7159168.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

rsync随机启动脚本 的相关文章

随机推荐

  • 经典面试题-大厂SQL题目

    1 如何用一个SQL找出部门下所有员工的平均工资大于某个数 例如20000元 的所有部门 正确答案 select depno avg sal as avgsal from emp group by depno having avgsal g
  • 使用python编写脚本测试目标主机的TCP端口连通性

    使用Python的Socket模块的connect 函数来尝试连接目标主机的特定端口 如果连接成功 则说明该端口是打开的 否则 该端口是关闭的 下面是一个示例脚本 可以检测目标IP的22端口是否开启 import socket def ch
  • 【c++】类模版

    1 类模板语法 类模板作用 建立一个通用类 类中的成员 数据类型可以不具体制定 用一个虚拟的类型来代表 语法 template
  • 市场监管总局关于对锂离子电池等产品实施强制性产品认证管理的公告

    按照 国务院办公厅关于深化电子电器行业管理制度改革的意见 国办发 2022 31号 有关要求 市场监管总局决定对电子电器产品使用的锂离子电池和电池组 移动电源以及电信终端产品配套用电源适配器 充电器 以下统称新纳入产品 实施强制性产品认证
  • 树莓派安装卸载软件命令apt-get

    apt get命令用法 1 安装软件 apt get install 软件名 2 卸载软件但不删除配置 apt get remove 软件名 3 卸载软件并且删除相关配置 apt get purge 软件名 4 更新数据库 apt get
  • Python简单的用户交互

    death age 80 name input your name input 接受的所有数据都是字符串 即便你输入的是数字 但依然会被当成字符串来处理 age input your age print type age int integ
  • TS复习----TS中的接口

    目录 概念 属性接口 函数类型接口 可索引的类型 类类型接口 接口继承 概念 接口的作用 在面向对象编程中 接口是一种规范的定义 他定义了行为和动作的规范 在程序设计里面 接口起到了一种限制和规范的作用接口定义了某一批类所需要遵守的规范 接
  • windos怎么查看oracle进程,怎么样查看哪个进程使用了哪个CPU

    1 在系统维护的过程中 随时可能有需要查看 CPU 使用率 并根据相应信息分析系统状况的需要 在 CentOS 中 可以通过 top 命令来查看 CPU 使用状况 运行 top 命令后 CPU 使用状态会以全屏的方式显示 并且会处在对话的模
  • java:方法引用无效-IDEA 社区版 lombok插件报错解决

    IDEA 社区版 lombok插件报错 java 方法引用无效 报错信息1 java 方法引用无效 找不到符号 符号 方法 getId 位置 类 com xxx xxxx className 打开problem面板向上找你就会发现还有一个报
  • 天九共享赋能新基建项目,易保全区块链存证助力应用场景多点开花

    在国家政策的大力扶持下 中国的区块链发展势力愈发迅猛 作为数字经济的基石 区块链技术发挥着重要作用 据数据显示 2020年全球区块链专利累计达到5 14万件 其中中国累计申请了3 01万件 占全球总数的58 同时 近期发布的 北京城市副中心
  • C语言写网络爬虫总体思路

    使用C语言编写爬虫可以实现网络数据的快速获取和处理 适用于需要高效处理海量数据的场景 与其他编程语言相比 C语言具有较高的性能和灵活性 可以进行底层操作和内存管理 适合处理较复杂的网络请求和数据处理任务 但是 使用C语言编写爬虫也存在一些挑
  • 2个不错的通配符比较函数

    近日在和朋友讨论 MaskMatch 时偶得2个不错的算法 函数1 只支持 模糊匹配 速度比采用递归算法的快近2倍 比TMask方法快很多 函数2 完全支持正规表达式 速度于之前的相同 不会正规表达式的朋友慎用 Funtion 1 Chec
  • mysql error1215

    You have a foreign key constraint operating in both directions When you re creating the tables the first to be created w
  • 基于STM32单片机的智能鱼缸的设计

    一 任务简介 本次以STM32F103单片机为核心 设计了一款智能鱼缸 能够实现智能温控 智能换水 智能供氧 智能喂食等功能 利用单片机作为主控制器 使用Keil软件进行程序开发 除STM32F103C8T6最小系统外 系统还包含温度传感
  • 【满分】【华为OD机试真题2023 JS】货币单位换算

    华为OD机试真题 2023年度机试题库全覆盖 刷题指南点这里 货币单位换算 时间限制 1s 空间限制 256MB 限定语言 不限 题目描述 记账本上记录了若干条多国货币金额 需要转换成人民币分 fen 汇总后输出 每行记录一条金额 金额带有
  • 数仓虚拟化技术:PieCloudDB Database 通过中国信通院 2023 「可信数据库」性能评测的强力支撑...

    可信数据库 是国内首个数据库的评测体系 被业界广泛认可为产品能力重要的衡量标准之一 PieCloudDB Database在该评测中展现出卓越的数据处理速度 稳定性和可扩展性 为用户提供了强大的数据分析和查询能力 6 月 15 16 日 中
  • EF Core Migration 报错:An error occurred using the connection to database ‘‘ on server ‘10.28.253.2‘

    EF Core Migration update database的时候 An error occurred using the connection to database on server 10 28 253 2 问题 在做EF Co
  • 嵌入式Linux构建yaffs根文件系统

    嵌入式Linux构建yaffs根文件系统 开发环境说明 ubuntu1404 i686 天嵌光盘里的交叉编译链 版本4 4 3 busybox 1 13 0 下载地址 https busybox net downloads 一 编译busy
  • TQ2440移植u-boot2016.11全过程记录-【1】单板建立并启动

    TQ2440移植u boot2016 11 单板建立并启动 移植说明 u boot2016 11是S3C2440最后一版的uboot支持 所以选择了此版本进行移植 交叉编译器使用的是天嵌官方的交叉编译器 版本为4 4 3 使用的ubuntu
  • rsync随机启动脚本

    服务端 1 bin sh 2 chkconfig 2345 21 60 3 description Saves and restores system entropy pool for 4 create by xiaohu 5 2014 0