I have the following batch file, which uses ADB to monitor device logs and searches for a string:

ECHO Starting log monitor...
START /B adb.exe logcat > log

(TYPE log | FIND "string to find") > NUL
IF "%errorlevel%" == "1" GOTO LOOP

ECHO String found!

The script starts the logcat command, which runs asynchronously and in the background, using START /B.

After the string is found, I would like to end the asynchronous logcat command, as it is no longer needed.

Is there any way of the main script telling the asynchronous script to end?

I know that I could technically use adb.exe kill-server or taskkill /F /IM adb.exe to end all ADB processes, but I need to only end the logcat command and continue running all other instances of ADB.

  • Use start with a title and then taskkill with a Windowtitle filter – DavidPostill Jan 5 at 17:56
  • @DavidPostill How do I get that to work? Running START "log_process" /B adb.exe logcat and then TASKKILL /FI "Windowtitle eq log_process" returns INFO: No tasks running with the specified criteria.. – Tikolu Jan 5 at 18:15
  • 1
    The windowtitle method works for me. adb may substitute your title by its own, so you need to use that instead. – harrymc Jan 5 at 20:32
@echo off

echo/ starting log monitor...
start /b adb.exe logcat >.\mylogcat.log

find "string to find" .\mylogcat.log >nul || (
   >nul timeout 15 /nobreak & goto loop ) 
echo/ string found!
wmic process where "name like '%%adb.exe%%' and commandline like '%%logcat%%'" delete

You can kill your process using the combination of the process name and part of the command line used to start it with wmic:

wmic process where "name like '%%adb.exe%%' and commandline like '%%logcat%%'" delete

You can use the operator || to keep your loop, and also add a timeout to that monitoring loop...

find "string to find" .\mylogcat.log >nul || (
   >nul timeout 15 /nobreak & goto loop ) 

In PowerShell using Get-WmiObject:

Get-WmiObject Win32_Process | % { if ($_.ProcessName -like '*adb.exe*' -and $_.CommandLine -like '*mylogcat.log*') {kill $_.ProcessId}}

Using Get-CimInstance Win32_Process:

Get-CimInstance Win32_Process -Filter "name='adb.exe'" | % { if ($_.CommandLine -like '*mylogcat.log*') {kill $_.ProcessId}}

# Or.... 
Get-CimInstance Win32_Process -Filter "name='adb.exe'" | 
% { if ($_.CommandLine -like '*mylogcat.log*') {kill $_.ProcessId}}

Additional resources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.