0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windowsのコマンドプロンプトで指定時間にシャットダウンか再起動するバッチ例

Posted at

単発小ネタ Windowsバッチ

狙った時間に端末を落としたい、という場合のコマンドプロンプトアプローチ例

機能

  • 実行すると再起動指定時間の入力を受け付けて、再起動をスケジュールしてみる。
schedule_reboot_v001.bat

@echo off
setlocal enabledelayedexpansion
title Reboot Scheduler

:: Logging setup
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd"') do set logdate=%%i
set logfile=%~dp0%logdate%_reboot_scheduler.log

for /f %%t in ('powershell -NoProfile -Command "Get-Date -Format HH:mm:ss"') do set nowLogTime=%%t
echo [%nowLogTime%] Reboot Scheduler start >> "%logfile%"

echo === Windows Reboot Scheduler ===
echo.

:: Prompt for time input
set /p targetTime=Enter reboot time (HH:MM, 24-hour format): 

:: Validate format
echo %targetTime% | find ":" >nul
if errorlevel 1 (
    echo  Invalid format. Please use HH:MM format. >> "%logfile%"
    echo  Invalid format. Please use HH:MM format.
    echo.
    pause
    exit /b 1
)

for /f "tokens=1,2 delims=:" %%a in ("%targetTime%") do (
    set /a hour=1%%a - 100
    set /a minute=1%%b - 100
)

if !hour! lss 0 (
    echo  Invalid hour: !hour! >> "%logfile%"
    echo  Invalid hour: !hour!
    echo.
    pause
    exit /b 1
)
if !hour! gtr 23 (
    echo  Invalid hour: !hour! >> "%logfile%"
    echo  Invalid hour: !hour!
    echo.
    pause
    exit /b 1
)
if !minute! lss 0 (
    echo  Invalid minute: !minute! >> "%logfile%"
    echo  Invalid minute: !minute!
    echo.
    pause
    exit /b 1
)
if !minute! gtr 59 (
    echo  Invalid minute: !minute! >> "%logfile%"
    echo  Invalid minute: !minute!
    echo.
    pause
    exit /b 1
)

:: Get current time with seconds
for /f "tokens=1-4 delims=:. " %%a in ("%time%") do (
    set /a nowH=1%%a - 100
    set /a nowM=1%%b - 100
    set /a nowS=1%%c - 100
)

:: Compute delay in seconds (add 5 seconds offset)
set /a nowTotal = nowH * 3600 + nowM * 60 + nowS
set /a targetTotal = hour * 3600 + minute * 60 + 5
set /a delay = targetTotal - nowTotal

if !delay! lss 0 (
    echo  Target time already passed. Current: !nowH!:!nowM!:!nowS! >> "%logfile%"
    echo  Target time already passed. Current: !nowH!:!nowM!:!nowS!
    echo.
    pause
    exit /b 1
)

:: Confirm schedule before execution
echo.
echo You are about to schedule a reboot at %targetTime% + 5 seconds.
set /p confirm=Do you want to proceed? (y/n): 
if /i not "!confirm!"=="y" (
    echo  User cancelled at confirmation step. >> "%logfile%"
    echo.
    echo  Canceled by user.
    echo.
    pause
    exit /b 1
)

:: Attempt to schedule reboot
echo.
echo Scheduling reboot in !delay! seconds...
shutdown /r /t !delay!
if errorlevel 1 (
    echo  Failed to schedule reboot. >> "%logfile%"
    echo.
    echo  Failed to schedule reboot. Another shutdown or reboot may already be scheduled.
    echo  You can cancel it with: shutdown /a
    echo.
    pause
    exit /b 1
)

:: Log success
for /f %%t in ('powershell -NoProfile -Command "Get-Date -Format HH:mm:ss"') do set nowLogTime=%%t
echo [%nowLogTime%] Reboot scheduled for %targetTime% + 5 sec (in !delay! sec) >> "%logfile%"

echo.
echo  Reboot successfully scheduled.
echo  To cancel: run "shutdown /a"
echo.
pause
exit /b 0

その他

  • 再起動じゃなくてシャットダウンにしたい場合、オプションを/r → /sに変更してください。
  • 何時に端末落としたい、という場合、タスクスケジューラ利用せずに、コマンドだけだと「何秒後」という指定しかできないので面倒。良い感じにダイアログで時刻指定で受け付けてほしいと思ったのでやってみた。
  • 今回は日跨ぎのスケジュールはできない仕様です。
  • キャンセルしたい場合は「shutdown /a」打つだけだけど、同じ感じで作成してみた。
cancel_reboot_v001.bat

@echo off
title Reboot Canceller

echo === Reboot Canceller ===

:: Confirm cancel before execution
echo.
echo You are about to cancel a reboot.
echo.
set /p confirm=Do you want to proceed? (y/n): 
if /i not "%confirm%"=="y" (
    echo.
    echo  Cancel bat canceled by user.
    echo.
    pause
    exit /b 1
)

:: Attempt to cancele reboot
shutdown /a
if errorlevel 1 (
    echo.
    echo  Failed to cancel reboot. No shutdown or reboot may be scheduled.
    echo.
    pause
    exit /b 1
)

echo.
echo Reboot has been canceled.
echo.
pause
exit /b 0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?