Search code examples
batch-filekill-processtasklist

Kill process in batch file that matches certain name and user


I have a question for ya'll...hopefully someone can help.

Background: I have, lets say, 6 .exe's that are launched by 6 separate scheduled tasks (the .exe's are named after a client and look like this - CLIENTNAME_PROGRAM.EXE). Those 6 tasks run under 3 separate user accounts for scheduling purposes. Meaning, I can have 2 processes running at the same time under different accounts (you can't run them simultaneously under one ID). If you use one ID, you have to run one, wait for it to end, then wait for the next one.

Issue: I have some code in my batch file which looks for another process and waits for it to end...however, this was written when the .exe name was the same for all processes (used to be just PROGRAM.EXE before we went to CLIENTNAME_PROGRAM.EXE). Problem is, I just realized that my %TFProName% variable is set to the name of the client that is CURRENTLY trying to run...so it's not looking for any other TFProName's running under the same computer account that are currently stuck. Does that make sense?

So, I need to:

Before starting CLIENT2_PROGRAM under user ACCOUNT1, search for any other *_PROGRAM process running under ACCOUNT1 and wait for it to end. If it runs long, run the powershell script to kill said process.

Below is my code snippet:

:PRVARCHSTART

tasklist /FI "IMAGENAME eq %TFProName%" | find /I "%TFProName%" >nul

IF ERRORLEVEL 1 (
   echo        **** %TIME% - Another archive process is not running...checking for an import/export          process... **** >> "%logfilefolder%\%logfilename%"
   goto PRVARCHEND
) ELSE (
   echo        **** %TIME% - Another archive is running...please wait...checking again... **** >>    "%logfilefolder%\%logfilename%"
   PING 1.1.1.1 -n 1 -w 5000 >nul
   powershell "& '%scriptdirectory%\KillTFProAdmin.ps1'"
   goto PRVARCHSTART
)
:PRVARCHEND

Solution

  • Here is the code I use now, which works great:

        :PRVARCHSTART
    
    tasklist /FI "IMAGENAME eq %TFProName%" | find /I "%TFProName%" >nul
    
    IF ERRORLEVEL 1 (
       echo        **** %TIME% - Another archive process is not running...checking for an import/export          process... **** >> "%logfilefolder%\%logfilename%"
       goto PRVARCHEND
    ) ELSE (
       echo        **** %TIME% - Another archive is running...please wait...checking again... **** >>    "%logfilefolder%\%logfilename%"
       PING 1.1.1.1 -n 1 -w 5000 >nul
       powershell "& '%scriptdirectory%\KillTFProAdmin.ps1'"
       goto PRVARCHSTART
    )
    :PRVARCHEND