Batch file - Closing separate cmd windows opened by a batch file process - windows

I am putting together a batch file which does the following:
Start Selenium test hub
Start Selenium test node
Run Selenium test script
Open test result xml doc.
Close Selenium hub and node
The Selenium hub and node are opened by calling 2 batch files using the START command so they open in their own cmd windows. I am all good with my process up to point 5), closing the two cmd windows containing the hub and node.
I know you can close all "cmd.exe" processes but that seems way to much of a blunt instrument. Is there a way of identifying or keeping a handle on the hub and node cmd windows opened by my batch script so that I can then just close those two only?
Thanks.

As an alternative to the wonderful npocmaka answer, if you know or can set the title of the started Selenium windows, can use this information to retrieve and filter the list of tasks with those titles.
When output to console is correct, remove the echo command from taskkill line
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure list of window titles
set titles="seleniumHUB" "seleniumNODE"
rem For each title in title list
for %%t in (%titles%) do (
rem Get the list of task with the indicated window title.
rem The list is get from tasklist, in verbose mode and in csv format
rem The last column in the list is the window title
for /f "tokens=1,2 delims=," %%a in (
'tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr /i /r /c:"%%~t[^,]*$"'
) do (
echo taskkill /pid %%~b
)
)
rem Clean
endlocal

Try something like this:
#echo off
for /f "tokens=2 delims=;= " %%a in ('wmic process call create "cmd.exe /c C:\selenium.bat"^,"c:\workdir" ^| find "ProcessId"') do (
set "cmd_pid=%%a"
)
taskkill /PID %cmd_pid% /f
with wmic process call create "cmd.exe /c C:\selenium.bat" you can start a process and get its PID and when you to kill it you can use taskkill /PID %cmd_pid% /f

Related

Trying to kill tasks that have been started in a file

So I am trying to start a butch of programs at once but then I want to to be able to kill all those programs at a later time. So I have created two bat files one to start the programs in the file and one to stop them.
This is the bat file that should stop them.
for /r "." %%a in (*.exe) do taskkill /IM %%~nxI
pause
It gives me an error that file was not found here is the error that it gave me. > ERROR: Invalid argument/option - 'D:\FileName'
Here is the code that starts them:
for /r "." %%a in (*.exe) do start "" "%%~fa"
pause
To kill all the tasks in the file:
for /r "." %%a in (*.exe) do taskkill /IM "%%~nxa"
pause

How do query and then kill a service in a batch file (newbie)

I am trying to kill specific services using the PID from "SC QUERYEX wuauserv". But I don't know how to pull the PID shown in the results to then run "taskkill /pid /f [PID]". I am trying to make a batch file which I can use on multiple machines remotely.
I have tried a couple of suggestions made in other similar questions found on google, but wuauserv is not being killed for some reason.
# echo off
cmd /c FOR /F "usebackq tokens=2 skip=3" %%i IN (tasklist /fi "services eq wuauserv") DO taskkill /PID %%
pause
The above is what I have, but it's not finding the specific service in the task list. Can anyone assist?
Essentially, because you specified usebackq you need to put your Command within backticks ie: IN (`Command`) DO () you also need to include the variable letter you specified earlier in the ending section which you have, right now it is just %% given you set up i as the variable letter it should be %%i.
Also the CMD /C portion is just not needed at all.
Thats said, just drop the UseBackQ it isn't necessary, here is yoru code cleaned up a little.
#(
SETLOCAL EnableDelayedExpansion
echo off
)
FOR /F "Tokens=2" %%I IN ('
Tasklist /fi "Services eq wuauserv"
^| FIND /I "wuauserv"
') DO (
ECHO Killing PID %%I
Taskkill /PID %%I
)
PAUSE

How to close cmd in batch file

start cmd /k
/k: is compulsory which will execute.
launching many command propmts can be done as below.
start cmd /k Call rc_hub.bat 4444
start cmd /k Call rc_grid1.bat 5555
start cmd /k Call rc_grid1.bat 6666
start cmd /k Call rc_grid1.bat 5570.
I want to know what cmd to be used for closing after launching
Eg: if %a="1234" start cmd /k Call rc_grid1.bat --> This is opening cmd , what cmd needs to give to close ?
Closing multiple instances of cmd is not a very uncommon task.
It can arise, for example, when debugging some complex set of batch scripts.
There is one of the ways to do this:
First, give cmd windows unique titles. For example,
start "unique_title" cmd.exe /k ...
Second, when you want to close them, get process ids from tasklist output matching the titles. Then kill those ids with taskkill.
tasklist /v /fo csv | findstr "unique_title"
Here is the full example, the first argument is the title substring to match:
kill_title.cmd
#echo off
set TITLE=unique_title
if not "%~1"=="" set "TITLE=%~1"
for /f "tokens=1,2,* delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr "%TITLE%"') do (
rem here we check that the first column is "cmd.exe" ... just in case
if "%%~a"=="cmd.exe" echo taskkill /pid "%%~b"
)
First check the script output, and if it is ok, remove echo before taskkill.
When messing with command prompts, I kept crashing my computer when command prompts got REALLY out of control. I made another command prompt as follows:
Title END
:End
taskkill /FI "WINDOWTITLE ne END" /IM cmd.exe /F /T
Goto :End
This will force close all command prompts that are running, not including this one, and will repeat the command until you close it.

fix loop to save essential windows processes, with its path, and kill the rest processes

I want to save the some system processes, with its path, to ensure that the system does not crash, and kill the rest of process
example: lsass.exe, winlogon.exe, conhost.exe, rundll32.exe, etc
This is my .bat:
set proc=,
:: proc
call:proc "lsass.exe"
call:proc "winlogon.exe"
call:proc "conhost.exe"
call:proc "rundll32.exe"
for /f "skip=3 tokens=1 delims= " %%a in ('tasklist /fi "username eq %username%"') do (
echo %proc%, | findstr /c:,%%a, 1>nul
if errorlevel 1 (
taskkill /f /im %%a /t
) else (
echo not kill
)
)
:: funcion proc
#echo off
pause
goto:eof
:proc
set getproc=%1
for /f "tokens=1 delims=," %%F in ('tasklist /nh /fi "imagename eq %getproc%" /fo csv') do set proc=%proc%,%%~F>nul
goto:eof
The problem is that my script does not save the path of the process, then, if there is a fake process running in another location, my script saves both processes. That's why I need to save the Windows system process including its original path
Example real process:
wmic process where "name='lsass.exe'" get ExecutablePath
Out real process:
C:\Windows\system32\lsass.exe
Example fake process:
Out XP:
C:\Documents and Settings\User\Local settings\Application Data\lsass.exe
or Out 7
C:\Users\User\AppData\Roaming\lsass.exe
c:\Users\User\Local Setting\Temp\lsass.exe
c:\Users\User\AppData\Local\lsass.exe
Note: Fake processes can be run from any path (.exe files associated with false process can be stored anywhere on the PC), except system folders (% windir%/system32 %windir%/sysWOW64 %windir%, etc)
Unfortunately, until now, My script does not close the fake processes, and only I could close manually using Process Explorer
request: What I need is to save the real processes, with its original path (lsass.exe, winlogon.exe, etc), and kill the rest. Thanks
Check this solution, by #JosefZ
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
REM note double quotes REM added for debugging ↓↓↓↓↓↓↓↓↓↓↓↓
set "_var="%userprofile%","%Appdata%","%HOMEPATH%","%homedrive%\ProgramData","D:\Remote""
REM added for debugging ↑↑↑↑↑↑↑↑↑↑↑↑
REM wmic requires double backslashes in specified path
set "_var=%_var:\=\\%"
for %%G in (%_var%) do (
rem echo processing %%G
REM used `GET Caption` for debugging
rem WMIC PROCESS WHERE "Name = '%~1' and ExecutablePath Like '%%%%~G%%'" GET Caption
REM operational
WMIC PROCESS WHERE "Name = '%~1' and ExecutablePath Like '%%%%~G%%'" Call terminate
)
Just try using WMIC directly.
#echo off
call :proc "lsass.exe"
call :proc "winlogon.exe"
call :proc "conhost.exe"
call :proc "rundll32.exe"
call :proc "services.exe
exit/b
:proc
WMIC PROCESS WHERE "Name='%~1' AND ExecutablePath Like '%%\\AppData\\%%'" CALL Terminate
[Edit /]
Because it is impossible to determine every single process which may or may not suit your purpose, the following, typed into the cmd prompt window, should close every single process which is not located within an exact path containing \Windows\ or \Program Files. Use it at your own peril.
WMIC PROCESS WHERE "NOT ExecutablePath LIKE '%\\Windows\\%' AND NOT ExecutablePath LIKE '%\\Program Files%'" CALL TERMINATE

Manage a process inside a cmd file

I would like to manage a process inside a cmd file. My cmd will launch an other cmd file and i would like the update status, ... For example, i would like to write the date when the other process is closed.
So, i tried :
rem %process% is the path to my cmd file
start "Process" %process%
Is there any way to manage the started process ?
I tried to use :
for /F "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq %process%"') do (
set MyPID=%%b
)
The problem is that the process is a cmd file so the IMAGENAME will be "cmd.exe".
Any solutions ?
it will be easier for you to get the pid when you are starting the process.
You can do this with WMIC command (though it's a little bit cumbersome):
#echo off
for /f "skip=5 tokens=* delims=" %%a in ('WMIC process call create "notepad.exe"^,"c:\"') do (
for /f "tokens=2 delims=;= " %%# in ("%%a") do if not defined pid set "pid=%%#"
)
echo %pid%
You can use also this ready to use script which can spare you some work (hope the help message is descriptive enough)
instead of IMAGENAME look for WINDOWTITLE (you defined that as a parameter to start):
tasklist /fi "windowtitle eq "Process"
use this in your existing for loop.
you can add /v to tasklist to get a verbose output, but you don't really need it for this task.

Resources