Ubuntu 20.04 在带有 MacOS 主机的 VirtualBox 中运行
使用python
而不是 shell 脚本。对于linux
在子进程中运行的命令。日期已更新,但在日期/时间自动重置后不久。已禁用 NTP。如何防止日期被重置回实际日期?
import re, shutil, csv, subprocess, yaml, click
from pathlib import Path
def cmd(cmd:str="ls -al", assertfail=True) :
up = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
str = [o.rstrip().decode() for o in up.stdout]
exitcode = up.wait()
if assertfail: assert exitcode == 0, f"[{exitcode}] {cmd} {str}"
return exitcode, str
if __name__ == '__main__':
# set date back before flash EOL date
import datetime as dt
n = dt.datetime.now()
if n.year > 2020:
systime = dt.datetime(n.year-1, n.month, n.day, n.hour, n.minute, n.second, n.microsecond).isoformat(sep=' ', timespec='milliseconds')
cmd("sudo timedatectl set-ntp off")
cmd(f"sudo date --set=\"{systime}\"")
# cmd("sudo hwclock --systohc")
cmd(f"sudo timedatectl set-time \"{systime}\"")
e, out = cmd("sudo timedatectl status")
print("\n".join(out))
e, out = cmd("date")
print(f"updated time {dt.datetime.now()} {out[0]}")
输出
Local time: Fri 2020-01-24 21:55:05 UTC
Universal time: Fri 2020-01-24 21:55:05 UTC
RTC time: Fri 2020-01-24 21:55:05
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
updated time 2020-01-24 21:55:05.980812 Fri 24 Jan 2020 09:55:05 PM UTC