Last active
January 3, 2016 18:59
-
-
Save vovcacik/8505761 to your computer and use it in GitHub Desktop.
Runs a command when a specified process is idle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal enabledelayedexpansion | |
if "%~1" == "" goto help | |
if "%~2" == "" goto help | |
set idle=0 | |
set process=%~1 | |
set command=%* | |
set command=!command:*%1 =! | |
:loop | |
:: Get IO and CPU usage and trim empty lines. | |
for /F "usebackq tokens=1,2 skip=1" %%o in (`wmic path win32_perfformatteddata_perfproc_process where (Name^='%process%'^) get IODataOperationsPerSec^, PercentProcessorTime ^| findstr /irc:"."`) do ( | |
:: Aggregate usage data. | |
set /A usage=%%o + %%p | |
:: Count consecutive idle occurrences only. | |
if "!usage!" == "0" ( | |
set /A idle=!idle!+1 | |
) else ( | |
set idle=0 | |
) | |
:: Run command after at least 30 seconds of idling. | |
if !idle! GTR 3 ( | |
endlocal | |
%command% | |
exit /B | |
) else ( | |
ping -n 11 localhost >nul && goto loop | |
) | |
) | |
:: Something went wrong. | |
echo. | |
echo Error occurred while running `%0 %*` | |
exit /B 1 | |
:help | |
echo Runs "command" when all processes with name "process" are idle for at least 30 seconds. | |
echo. | |
echo Usage: idling process command | |
echo Example: idling Dropbox shutdown /s /t 60 | |
exit /B 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment