linux shell脚本中执行ntpclient

#!/bash/sh
# Program:
#         execute ntpclient cmd 
# Version: v1.0
# Author: hp
#
function syntime() {
    echo "synchronizing time...."
    echo "server is $1"
    echo "execute cmd..."
    if test -z $1; then
        echo "server is $1"
        h=""
    fi
    if test -z $count; then
        echo "count is $count"
        c=""
    fi
    if test -z $interval; then
        i=""
    fi
    echo "$ntpd $h $1 $c $count $d $s $i $interval"
    $ntpd $h $1 $c $count $d $s $i $interval
}

start_time="`date +%Y%m%d`"
ntpd="/home/haopeng/open_source/ntpclient-2015/ntpclient"
declare -f syntime
echo "start time is $start_time"
array=($@)
echo ${array[*]}
echo "total element count is $#"
for ((i=0; i<$#; i=i+1))
do
    sleep 1
    echo ${array[i]}
    case ${array[i]} in
    "-h")
    i=$(($i+1))
    h="-h"
    server=${array[i]}
    echo "server is ${array[i]}"
    ;;
    "-c")
    c="-c"
    i=$(($i+1))
    count=${array[i]}
    echo "count is ${array[i]}"
    ;;
    "-i")
    i="-i"
    i=$(($i+1))
    interval=${array[i]}
    echo "interval is ${array[i]}"
    ;;
    "-s")
    s="-s"
    ;;
    "-d")
    d="-d"
    ;;
    esac
done
echo "now time is $(date +%Y%m%d)"

echo $server | grep ","
if [ $? -eq 0 ]; then
    echo "there is muti server"
    server_split=${server//,/ }
    while [ true ]        
    do
    for everyserver in $server_split
    do
        syntime $everyserver
        if [ $start_time != `date +%Y%m%d` ]; then
            echo "time is synchronized!!"
        fi
        while [ true ]
        do
            echo "check ntpclient process whether exit or not"
            if [ `ps -ef | grep "ntpclient" | wc -l` -eq 1 ]; then    
                echo "ntpclient is exit.."
                break
            else
                echo "ntpclient is not exit and wait to continue check."
                sleep 1    
            fi
        done
    done
    echo "all server is polling and wait sleep 60 second.."
    sleep 60
    done
else
    echo "there is one server"
    syntime $server
fi


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值