您好,欢迎访问一九零五行业门户网

rsync随机启动脚本

服务端
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 starting11 /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf12 sleep 213 myport=`netstat -lnt|grep 873|wc -l`14 if [ $myport -eq 2 ]15 then16 action rsync start /bin/true17 else18 action rsync start /bin/false19 fi20 ;;21 stop)22 echo rsync is stoping23 myport=`netstat -lnt|grep 873|wc -l`24 if [ $myport -eq 2 ]25 then 26 killall rsync &>/dev/null27 sleep 228 killall rsync &>/dev/null29 sleep 130 fi31 myport=`netstat -lnt|grep 873|wc -l`32 if [ $myport -ne 2 ]33 then34 action rsync stop /bin/true35 else36 action rsync stop /bin/false37 fi38 ;;39 restart)40 if [ `netstat -lnt|grep 873|wc -l` -eq 0 ]41 then42 /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf43 sleep 244 myport=`netstat -lnt|grep 873|wc -l`45 if [ $myport -eq 2 ]46 then47 action rsync restart /bin/true48 else49 action rsync restart /bin/false50 exit51 fi52 else53 killall rsync &>/dev/null54 sleep 255 killall rsync &>/dev/null56 sleep 157 /usr/local/rsyncd/bin/rsync --daemon --config=/etc/rsyncd.conf58 sleep 259 myport=`netstat -lnt|grep 873|wc -l`60 if [ $myport -eq 2 ]61 then62 action rsync restart /bin/true63 else64 action rsync restart /bin/false65 fi66 fi67 ;;68 status)69 myport=`netstat -lnt|grep 873|wc -l`70 if [ $myport -eq 2 ]71 then72 echo rsync is running73 else74 echo rsync is stoped75 fi76 ;;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 rsync100 if [ -s $rsync_pid_file ] && kill -0 $(cat $rsync_pid_file) >/dev/null 2>&1; then101 log_progress_msg apparently already running102 log_end_msg 0103 exit 0104 fi105 rsync_start106 else107 if [ -s $rsync_config_file ]; then108 [ $verbose != no ] && log_warning_msg rsync daemon not enabled in $rsync_defaults_file, not starting...109 fi110 fi111 ;;112 stop)113 log_daemon_msg stopping rsync daemon rsync114 start-stop-daemon --stop --quiet --oknodo --pidfile $rsync_pid_file115 log_end_msg $?116 rm -f $rsync_pid_file117 ;;118 119 reload|force-reload)120 log_warning_msg reloading rsync daemon: not needed, as the daemon121 log_warning_msg re-reads the config file whenever a client connects.122 ;;123 124 restart)125 set +e126 if $rsync_enable; then127 log_daemon_msg restarting rsync daemon rsync128 if [ -s $rsync_pid_file ] && kill -0 $(cat $rsync_pid_file) >/dev/null 2>&1; then129 start-stop-daemon --stop --quiet --oknodo --pidfile $rsync_pid_file || true130 sleep 1131 else132 log_warning_msg rsync daemon not running, attempting to start.133 rm -f $rsync_pid_file134 fi135 rsync_start136 else137 if [ -s $rsync_config_file ]; then138 [ $verbose != no ] && log_warning_msg rsync daemon not enabled in $rsync_defaults_file, not starting...139 fi140 fi141 ;;142 143 status)144 status_of_proc -p $rsync_pid_file $daemon rsync145 exit $? # notreached due to set -e146 ;;147 *)148 echo usage: /etc/init.d/rsync {start|stop|reload|force-reload|restart|status}149 exit 1150 esac151 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格式的, 然后存盘退出。
再次运行脚本。
以上就是rsync随机启动脚本的详细内容。
其它类似信息

推荐信息