Case5:Rsync网络服务启动脚本--通过rsync进程判断

18 篇文章 ¥29.90 ¥99.00
本文介绍了如何创建并配置`rsyncd_fun.sh`脚本,使其成为系统开机启动的一部分。首先,将脚本复制到`/etc/init.d`目录并赋予执行权限,然后通过测试命令启动、停止和重启服务。最后,使用`chkconfig`添加并启用开机启动项。

1 脚本内容 rsyncd_fun.sh

#!/bin/bash
#chkconfig: 2345 20  80
#description: start rsync and stop rsync scripts 

. /etc/init.d/functions

function usage(){
        echo $"usage:$0 {start|stop|restart}"
        exit 1
}

function start(){
        rsync --daemon
        sleep 1
        if [ `netstat -lntup|grep rsync|wc -l` -ge 1 ];then
                action "rsyncd is started." /bin/true
        else
                action "rsyncd is started." /bin/false
        fi
}

function stop(){
        killall rsync
        sleep 1
        if [ `netstat -lntup|grep rsync|wc -l` -eq 0 ];then
                action "rsyncd is stopped." /bin/true
        else
                action "rsync is started." /bin/false
        fi
}

function main(){
        if [ $# -ne 1 ];then
脚本代码如下: ┌──(root㉿kali)-[/home/liangjian] └─# cat /etc/init.d/lj_rsync #! /bin/bash # liangjian # -ne if条件语句的不等于 # $# 返回参数的个数 # $0 取得的己脚本文件名称 if [ "$#" -ne 1 ] then echo "usage:$0 {start|stop|restart}" exit 1 fi # 当用户的选择是启动rsync时: if [ "$1" = "start" ] then /usr/bin/rsync --daemon sleep 2 if [ `netstat -tunlp |grep rsync |wc -l -gt 1` ] then echo "Rsync is started" exit 0 fi elif [ "$1" = "stop" ] then killall rsync &> /dev/null sleep 2 if [ `netstat -tunlp | grep rsync | wc -l -eq 0` ] then echo "Rsync is stoped" exit 0 fi elif [ "$1" = "restart" ] then killall rsync sleep 2 killpro=`netstat -tunlp |grep rsync |wc -l` #kill掉以后,服务和端口都没有了,没有输出,wc -l 打印为0 /usr/bin/rsync --deamon #启动rsync服务的命令 sleep 2 startpro=`netstat -tunlp |grep rsync |wc -l` if [ "killpro" -eq 0 -a "startpro" -gt 1 ] # -a 表示and -gt 大于等于 then echo "Rsync is restarted" exit 0 fi else echo "usage:$0 {start|stop|restart}" exit 1 fi 但是执行报错如下: ┌──(root㉿kali)-[/home/liangjian] └─# /etc/init.d/lj_rsync start wc: 无效的选项 -- g 请尝试执行 "wc --help" 来获取更多信息。 ┌──(root㉿kali)-[/home/liangjian] └─# /etc/init.d/lj_rsync stop wc: 无效的选项 -- e 请尝试执行 "wc --help" 来获取更多信息。 ┌──(root㉿kali)-[/home/liangjian] └─# /etc/init.d/lj_rsync restart rsync: 未找到进程 rsync: --deamon: unknown option rsync error: syntax or usage error (code 1) at main.c(1782) [client=3.4.1] /etc/init.d/lj_rsync: 第 47 行:[: killpro: 需要整数表达式 请给出具体是哪里错了,给出修复方法。
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值