服務器超售到不行,cpu時間隔一段時間就滿好多。嘗試配置了標準了自動的時間同步,效果不理想。於是用python調用worldtimeapi.org的api配合crontab來修正系統時間。
import subprocess,requests,time
def main():
try:
response = requests.request("GET", "http://worldtimeapi.org/api/timezone/Asia/Hong_Kong")
timeJson=response.json()
timeAbs = abs(timeJson['unixtime']-time.time())
if timeAbs > 15 :
subprocess.call(['date','+%T','-s',timeJson['datetime'][11:19]])
print("time synced")
else:
print(timeAbs)
except:
print("net error")
if __name__ == '__main__':
main()
#添加crontab任務 crontab -e #每兩小時檢查一次 0 */2 * * * /usr/local/bin/python3 /home/42/time.py >> /home/42/t.log 2>&1