#/bin/bash # beforTime=02:00 afterTime=08:00 beforTime=$(echo $beforTime | awk -F : {‘print $1‘}) afterTime=$(echo $afterTime | awk -F : {‘print $1‘}) Flag=0 while true do sleep 2s echo "date: $(date +"%Y-%m-%d %H:%M:%S")" if [ $(date +%H) -eq $beforTime ]; then CurDate=$(date +%d) ChangeDate=$(expr $CurDate - 1) date -s ‘$(date +"%Y-%m-$ChangeDate %H:%M:%S")‘ if [ $? -eq 0 ]; then echo "Changed date: $(date +"%Y-%m-$ChangeDate %H:%M:%S")" Flag=1 break fi fi done if [ $Flag -eq 1 ];then while true do sleep 2s echo "date: $(date +"%Y-%m-%d %H:%M:%S")" if [ $(date +%H) -eq $afterTime ]; then CurDate=$(date +%d) ChangeDate=$(expr $CurDate + 1) date -s ‘$(date +"%Y-%m-$ChangeDate %H:%M:%S")‘ if [ $? -eq 0 ]; then echo "Changed date: $(date +"%Y-%m-$ChangeDate %H:%M:%S")" Flag=0 break fi fi done fi
@echo off @set StartDate=05 @set ChangeDate=04 @set ChangeTime=18:59 @set endTime=19:00 @echo 开始任务:05号1点更改时间 @set ErrNum=0 :ProcessDate @if %ErrNum% EQU 1 (ping /n 5 127.0.0.1 > nul) @for /f "usebackq tokens=1,2,3 delims=-/ " %%i in (`date /t`) do ( set Year=%%i set Mon=%%j set TodayDate=%%k echo Real_Date: %%i-%%j-%%k %TodayDate% ) @if %TodayDate% NEQ %StartDate% ( set ErrNum=1 goto :ProcessDate ) :ProcessTime @if %TodayDate% EQU %StartDate% ( for /f "usebackq tokens=1,2 delims=-/ " %%a in (`time /t`) do ( if "%%a" NEQ "%ChangeTime%" goto :ProcessTime if "%%a" == "%ChangeTime%" call :ChangeCurDate :ProcessTime_2 for /f "usebackq tokens=1,2 delims=-/ " %%a in (`time /t`) do ( if "%%a" == "%endTime%" ( echo %Year%-%Mon%-%TodayDate% date %Year%-%Mon%-%TodayDate% goto :eof ) else ( goto :ProcessTime_2 ) ) ) ) :ChangeCurDate echo Change_Date: %Year%-%Mon%-%ChangeDate% @date %Year%-%Mon%-%ChangeDate% @goto :eof
Linux Windows 定时修改系统时间
时间: 2024-10-11 01:12:20