Skip to content

Instantly share code, notes, and snippets.

@shenqihui
Last active April 13, 2023 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shenqihui/7dadcd07111f72e03966334016f81faf to your computer and use it in GitHub Desktop.
Save shenqihui/7dadcd07111f72e03966334016f81faf to your computer and use it in GitHub Desktop.
系统对时脚本

时间对时:

all_host.txt 参考 pssh 的参数来写

下载这个 https://github.com/angeloc/htpdate 并且解压到 cd /root/htpdate-1.2.6

prsync -h all_host.txt /root/htpdate-1.2.6/ /root/htpdate-1.2.6/

pssh -h all_host.txt 'timedatectl set-local-rtc 0'

pssh -h all_host.txt 'systemctl stop ntpd'

pssh -h all_host.txt 'systemctl disable ntpd'

pssh -h all_host.txt 'yum remove -y ntpdate'

pssh -h all_host.txt 'timedatectl set-timezone Asia/Shanghai' pssh -h all_host.txt 'cd /root/htpdate-1.2.6 && make ENABLE_HTTPS=1 && make install'

pssh -h all_host.txt 'htpdate -a -t http://www.baidu.com && hwclock -w'

pssh -P -h all_host.txt 'htpdate -a -t http://www.baidu.com && hwclock -w && date' | grep -v root | grep -v correction

编辑跳板机: /root/time.sh

cDate=$(wget http://www.baidu.com --debug -o /tmp/time.html && cat /tmp/time.html |  grep Date | head -n 1)
cDate=${cDate/Date:/}
echo $cDate
date -s "$cDate"
hwclock -w

prsync -h all_host.txt /root/time.sh /root/time.sh

不怎么准确

pssh -h all_host.txt 'bash /root/time.sh'

htpdate 进行精准化

pssh -h all_host.txt 'htpdate -a -t http://www.baidu.com && hwclock -w' pssh -P -h all_host.txt 'date' | grep -v root

增加定时任务

0 4 * * * root parallel-ssh -h /root/all_host.txt 'bash /root/time.sh'

0 4 * * * root parallel-ssh -h /root/all_host.txt 'htpdate -a -t http://www.baidu.com && hwclock -w'

cDate=$(wget http://www.baidu.cn --debug -o /tmp/time.html && cat /tmp/time.html | grep Date | head -n 1)
cDate=${cDate/Date:/}
echo $cDate
date -s "$cDate"
hwclock -w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment