Introduction
Windows Batch can monitor running processes, check CPU usage, detect crashes, restart apps, and log system activity. This tutorial covers tasklist, wmic, PowerShell bridging, and loop monitoring.
1. Check If a Process Is Running
tasklist /fi "imagename eq notepad.exe"
2. Check and React in Batch
tasklist /fi "imagename eq notepad.exe" | find "notepad.exe" >nul
if %errorlevel%==0 (
echo Notepad is running
) else (
echo Notepad is NOT running
)
3. Auto-Restart Program if It Crashes
@echo off
:loop
tasklist | find "myapp.exe" >nul
if errorlevel 1 (
echo Restarting app...
start "" "C:\path\myapp.exe"
)
timeout /t 3 >nul
goto loop
4. Logging Process Status
echo %date% %time% >> log.txt
tasklist >> log.txt
echo ------------------------- >> log.txt
5. Kill a Process
taskkill /im chrome.exe /f
6. List Processes With Memory Usage
tasklist /fi "status eq running" /fo list
7. CPU Usage (via WMIC)
wmic cpu get loadpercentage
8. Get Process CPU Usage (WMIC)
wmic path win32_perfformatteddata_perfproc_process where name="chrome.exe" get PercentProcessorTime
9. Check RAM Usage
wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /value
10. Monitor in Real-Time (Loop)
@echo off
:loop
cls
echo CPU Load:
wmic cpu get loadpercentage
echo ----
echo RAM:
wmic OS get FreePhysicalMemory,TotalVisibleMemorySize
timeout /t 2 >nul
goto loop
11. Using PowerShell for Better Metrics
powershell "gps chrome | select name, cpu, id"
12. Combined Batch + PowerShell Monitor
:loop
cls
echo === Chrome Usage ===
powershell "gps chrome | select name, cpu, pm"
timeout /t 1 >nul
goto loop
13. Check If Program Stopped Responding
tasklist /fi "status eq not responding"
14. Auto-Kill Unresponsive Programs
for /f "skip=3 tokens=1" %%a in ('tasklist /fi "status eq not responding"') do (
taskkill /f /im %%a
)
15. Summary
- Use
tasklistto detect processes - Use loops to monitor continuously
- Use
wmicor PowerShell for CPU/RAM metrics - Can auto-restart crashed apps
- Can kill frozen applications
- Batch + PowerShell = best monitoring tools combo