0

I need help regarding batch script handling, help because working with on-board tools again, no extras installable.

I have a CMD.exe script that gathers information about running processes (via tasklist.exe), and pipes them tee like into a file, each line preceeded by a timestamp. This file is later used to generate some fancy graphs for all processes that are consuming memory or cputime above some threshold.

But the problem is, I have to start this script from my environment right before the loadtest is running, and need to stop it after the test is finished. Start and stop are not in one process and can be quite long running (from minutes to several hours).

I tried already to use the TITLE command to be able to identify the running script afterwards, to kill it with TASKKILL /F /FI "WINDOWTITLE EQ PSMON" but that does not work, the windowtitle seems not to be set if the script is not startet manualy.

For the interested, here is the script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
TITLE=PSMON

:START
SET FILE=%TEMP%\psmon.csv
ECHO TIMESTAMP^;IMAGENAME^;PID^;MEMKB^;CPUTIME>%FILE%

:LOOP
TITLE=PSMON
SET TS=%TIME%
SET SEK=%TS:~7,1%
IF %SEK% == 0 (
    FOR /F "tokens=1-12 delims=,:" %%A IN ('TASKLIST /V /NH /FO CSV') DO (
    SET IMAGE=%%A
    SET PID=%%B
    SET MEM=%%E
    SET CPUH=%%H
    SET CPUM=%%I
    SET CPUS=%%J
    SET /A CPU=!CPUH!*3600 + !CPUM!*60 + !CPUS!
    IF !CPU! GTR 100 (
        ECHO %DATE:~-4%%DATE:~-7,2%%DATE:~-10,2%%TS:~0,2%%TS:~3,2%%TS:~6,2%^;!IMAGE:~1,-1!^;!PID:~1,-1!^;!MEM:~1,-3!^;!CPU!>>%FILE%
        )
    )
)
GOTO LOOP

And to be clear, the question is, how can I identify the instance of this with windows on-board tools and kill the process if this is not startet directly on the cmdline nor using double-click (try out starting it by a second batch.cmd file using the start command).

I'd like something like getting the PID on start of the script and write it to a tempfile or anything like it is done on unix systems, but I doubt that there is something like this.

EDIT: Got hinted to a bunch of lines of code like these:

for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq %instance%" ^| find /i "PID:"`) do set PID=%%a
if not defined PID for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq Administrator:  %instance%" ^| find /i "PID:"`) do set PID=%%a
if not defined PID echo !Error: Could not determine the Process ID of the current script.  Exiting.& exit /b 1

Sadly they just don't work, the script always stops with the errormessage defined. Maybe I should mention, that I'm working on Win7?

1

Ok, thanks to DavidPostill and this link What is the PID of the current cmd.exe, there is really a working way to get the PID with on-board tools.

SET T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
SET /P PID=<%T%
ECHO %PID:~16%>%TEMP%\my.pid

With that I'll be able to kill the task later on.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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