declare -a hosts 声明数组
declare -i elements i  声明2个整形变量

readarray -t删除文本结尾的换行符

crontab条目如下:

10 * * * * /root/detection_tool/sync_time.sh /root/detection_tool/time_sync_hosts

我们用来多台服务器时间同步shell scripts如下:

#!/bin/bash

declare -a hosts
declare -i elements i


readarray -t hosts < $1
elements=${#hosts[@]}

for ((i=0;i<$elements;i++)); do
        echo "${hosts[${i}]}:"
        ssh -i /root/xxx.key root@${hosts[${i}]} 'ntpdate -u time.rightscale.com'
        ssh -i /root/xxx.key root@${hosts[${i}]} 'service ntpd restart'
done

exit 0