How to Fix: pbeagent.exe 100% CPU Usage (Kill, Respawn Script)

Dennis Faas's picture

Infopackets Reader Jill H. writes:

" Dear Dennis,

I purchased several uninterrupted power supply units dirt cheap for my home office (model APC 1500 SMX1500RM2U). They work great - 1500VA @ 48 volts and a nice digital display. I downloaded PowerChute Business Edition which uses 'pbeagent.exe' executable which is responsible for communicating between the UPS and my PC so that the PC can shut down safely during a power outage.

The issue I'm having is that every so often the pbeagent.exe crashes and causes high cpu usage. When this happens, my computer's CPU fan is noticeably louder, and since the process is no longer working, it can't communicate with my computer to safely shutdown the machine. If I manually delete the pbeagent.exe in Task Manager and restart APCPBEAgent via services.msc, it runs fine for a day or two but eventually it will get stuck again. Is there any way to prevent this from happening? I am willing to pay for the help because this needs to be fixed. "

My response:

I asked Jill if she would like to connect using my remote desktop support service, and she agreed.

Below I will discuss my findings.

How to Fix: pbeagent.exe 100% CPU Usage (Kill, Respawn Script)

After looking through many online forums about this issue it is suggested that the communication between pbeagent.exe (which runs on the PC) and the UPS is lost, which then causes pbeagent.exe to crash and become a stuck process.

To fix this, I made a custom batch script to monitor pbeagent.exe and kill it if it becomes stuck, which then causes the service to respawn.

Here's How My Script Works

If the CPU usage for pbeagent.exe exceeds 10%, the pbeagent.exe process is killed and is then automatically restarted by the APCPBEAgent service agent. A log is kept in the same folder as where the script runs, so you can see if it is executing and doing its job. The log will automatically truncate itself so it won't become a huge file. I did test the script and it's working 100%.

Some Important Caveats

You may need to test the script a few times to make sure it's working. The script below uses 10% as the default value for CPU threshold, so anything above 10% results in the pbeagent.exe process getting killed and respawn. The respawn doesn't happen immediately - it may take a minute or two because it's controlled by the APCPBEAgent service.

The easiest way to determine what your CPU threshold should be set to in the script is to wait until pbeagent.exe crashes, then see how much CPU it's eating by going to Task Manager -> Details tab, then sort processes by CPU usage, the modify the script accordingly (in red below).

Steps:

  1. Copy the Script Below

    Highlight the code below with your mouse, then right click over top of it and select "Copy":

    REM -- pbeagent.exe cpu threshold check by Dennis Faas / www.infopackets.com/contact
    REM -- this program checks to see if pbeagent.exe has high CPU usage and kills it
    REM -- pbeagent.exe will automatically restart via
    APCPBEAgent (services.msc)

    @echo off
    setlocal

    REM -- Paths

    set "LOGFILE=%~dp0pbeagent_monitor.log"
    set "TMP_PS=%TEMP%\pbe_cpu_check.ps1"
    set "TMP_OUT=%TEMP%\pbe_cpu_output.txt"

    REM -- CPU threshold

    set "THRESHOLD=10"

    REM -- Log start

    echo [%date% %time%] Script started. >> "%LOGFILE%"

    REM -- PowerShell to get real-time CPU usage

    > "%TMP_PS%" echo $proc = Get-CimInstance Win32_PerfFormattedData_PerfProc_Process ^| Where-Object { $_.Name -eq 'pbeagent' }
    >> "%TMP_PS%" echo if (-not $proc) { 'NA' } else { $proc.PercentProcessorTime }

    REM -- Run PowerShell and capture result

    powershell -NoProfile -ExecutionPolicy Bypass -File "%TMP_PS%" > "%TMP_OUT%"
    set /p CPU_USAGE=<"%TMP_OUT%"
    del "%TMP_PS%" >nul 2>&1
    del "%TMP_OUT%" >nul 2>&1

    REM -- Process not running

    if "%CPU_USAGE%"=="NA" (
    echo [%date% %time%] pbeagent.exe not running. >> "%LOGFILE%"
    goto TrimLog
    )

    REM -- Log CPU usage

    echo [%date% %time%] pbeagent.exe real-time CPU: %CPU_USAGE%%% >> "%LOGFILE%"

    REM -- Evaluate CPU usage

    set /a CPU_INT=%CPU_USAGE%
    if %CPU_INT% GTR %THRESHOLD% (
    powershell -NoProfile -Command "Stop-Process -Name pbeagent -Force" >nul 2>&1
    echo [%date% %time%] CPU usage exceeded %THRESHOLD%%% - process killed. >> "%LOGFILE%"
    ) else (
    echo [%date% %time%] CPU usage within acceptable limits - no action taken. >> "%LOGFILE%"
    )

    :TrimLog

    REM -- Trim log file to last 100 lines

    powershell -NoProfile -Command ^
    "$path = '%LOGFILE%'; if (Test-Path $path) { (Get-Content $path -Tail 100) | Set-Content $path }"

    endlocal
     
  2. Paste and Save the batch script in Notepad

    Click Start, then type in "notepad.exe" (no quotes); wait for Notepad to show up in the list and right click it and run as administrator.

    Right click in an empty space inside of notepad and select "Paste" from the dialogue menu.

    Click File -> Save As in Notepad and save the file as "pbeagent_cpu_threshold_check.bat" in the same folder where Powerchute Business Edition is stored (i.e., "C:\Program Files (x86)\APC\PowerChute Business Edition\agent\cmdfiles\pbeagent_cpu_threshold_check.bat").
     
  3. Run the script every 30 minutes using Task Scheduler

    Click Start, then type in "task scheduler"; wait for Task Scheduler to appear in the list, and click it.

    Inside Task Scheduler, click Action -> Create Task. Under the General tab: call it "UPS pbeagent threshold check" and enable: "Run whether user is logged on or not", "Run with highest privileges", and "Hidden".

    On the Triggers tab: click New -> Begin task: Daily (starting at the next half hour or hour); make sure "Enabled" has a check mark.

    On the Actions tab: click New -> Action: start a program; click Browse and locate pbeagent_cpu_threshold_check.bat and click it.

    On the Conditions tab: remove all options

    On the Settings tab: only check mark "Allow task to run on demand"

    Click OK.

You can test to see if it runs by right clicking on the new task you just created, and then review the log file. It should have executed.

Got More Tech Questions? Ask Dennis!

I can fix most problems remotely using my remote desktop support. Or, if you have a simple tech question you'd like me to answer like the one above, send me an email through the contact page and I'll consider writing an article about it.

About the author: Dennis Faas is the CEO and owner of Infopackets.com. Since 2001, Dennis has dedicated his entire professional career helping others with technology-related issues with his unique style of writing in the form of questions-and-answers; click here to read all 2,000+ of Dennis' articles online this site. In 2014, Dennis shifted his focus to cyber crime mitigation, including technical support fraud and in 2019, online blackmail. Dennis has received many accolades during his tenure: click here to view Dennis' credentials online DennisFaas.com; click here to see Dennis' Bachelor's Degree in Computer Science (1999); click here to read an article written about Dennis by Alan Gardyne of Associate Programs (2003). And finally, click here to view a recommendation for Dennis' services from the University of Florida (dated 2006).

Rate this article: 
Average: 5 (3 votes)