import os
import time
from datetime import datetime, timedelta
# 定义重启周期为21天
restart_period = 21
# 获取当前日期距离指定日期的剩余秒数
def seconds_until_next_restart():
current_time = datetime.now()
next_restart_time = (current_time + timedelta(days=restart_period)).replace(hour=0, minute=0, second=0)
seconds_until_restart = (next_restart_time - current_time).total_seconds()
return seconds_until_restart
# 主程序
if __name__ == '__main__':
while True:
seconds_until_restart = seconds_until_next_restart()
time_remaining = str(timedelta(seconds=seconds_until_restart))
# 清除上一次的输出内容
print('\r\033', end='')
print(f"距离下次重启还有 {time_remaining}", end='')
time.sleep(1) # 每隔一秒检查一次是否需要重启
if seconds_until_restart <= 0:
os.system("shutdown /r /t 1") # 执行重启命令
|