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.
Related
Henlo smarter folks.
While having fun with automating some workflows [im doing this in batch (/ vba)], btw this means if you think its better / easier to perform this in vba hit me, i faced the problem of using taskkill for multiple processes of the same name.
First i tried to circumvent this by using their pids's, but i figured that this wont work because it changes everytime a process restarts, at least the ones i look for do.
In short:
When ive got two or more processes of the same name with parameters like
example.exe -test abcd and
example.exe -testing;
How to kill only the first one?
In long ... :
For example, if i would do:
taskkill /f /im example.exe this would crash or bsod the machine etc, or do;
taskkill /PID example_pid this would prompt an error not found or maybe even kill the wrong process
The full batch looks like this:
test_autokill.bat
:start
timeout 5
taskkill /f /im example1.exe
taskkill /f /im example2.exe
taskkill /f /im *and so on*
goto start
Additionally i guessed that i would have to use tasklist first to output processes including their parameters, but searching to do so failed completely.
Also i am not sure about what i would have to do next especially because i wasnt able to perform if+else (i think it usually said 'dont know what else is') conditions with tasklist previously.
Thanks for your knowledge
EDIT 1
I was pointed to a related Topic which wanted to do the "opposide" of. Tho i tried it without the "not" modidier.
All tries (below) replied the same error Message:
ERROR:
Description = The request is invalid.
My tries (in AdminCLI):
wmic Path win32_process Where "CommandLine Like 'C:\Windows\System32\svchost.exe -k NetworkService'" Call Terminate
wmic Path win32_process Where "CommandLine Like 'C:\Windows\System32\svchost.exe -k NetworkService'" Delete
wmic Path win32_process Where "CommandLine Like 'C:\Windows\System32\svchost.exe -k NetworkService' and name='svchost.exe'" Call Terminate
wmic Path win32_process Where "CommandLine Like 'C:\Windows\System32\svchost.exe -k NetworkService' and name='svchost.exe'" Delete
The original ones had a typo btw, scv instead of svc.
With a batch file, you can try like this way if you want to kill the first PID, you just remove the echo before the Taskkill command in this example :
#echo off
Set "WMIC_CMD=WMIC Process Where "CommandLine Like '%%-k NetworkService'" get Handle /Value"
SetLocal EnableDelayedExpansion
#for /f "tokens=1* delims==" %%I in ('%WMIC_CMD%') do (
#for /F "delims=" %%K in ("%%J") do (
Set /A Count+=1
set "Handle[!Count!]=%%K"
)
)
#for /L %%i in (1,1,%Count%) do (
echo %%i - !Handle[%%i]!
)
pause
REM IF You want to kill the first one
ECHO Taskkill /F /PID !Handle[1]!
pause
exit /b
How are you all doing?
I made a batch file that gets the memory usage of a process that has a specific PID...
Here is the code:
#Echo off
REM Get memory from process using tasklist
for /f "tokens=2 delims=," %A in ('tasklist /svc /fi "SERVICES eq .Service02" /FO csv /NH') do (
REM Store PID on a variable
#Set "pidSlv02=%~A"
REM Get memory usage from process with that PID
for /f "tokens=5 delims= " %B in ('tasklist /fi "pid eq %pidSlv02%" /NH') do (
REM Store mem value on another variable
#Set "memSlv02=%~B"
REM Remove commas from variable
#Echo %memSlv02% K
)
)
So, the output is:
1,407,356
Now, i need to convert that number to be 1407356
I did not come here directly. I already tried a couple of things but without success.
Im still very new and learning CMD/batch scripts... Didnt even know t was possible to do so much on Windows. Thought it was only possible on Linux. So please don't be angry with me if i dont understand something. Im trying my best! 😣
Also, sorry if i mispelled something here since this is not my native language.
I tried adding the code:
set memSlv02=!memSlv02:,=!
set /a total=!total! + !memSlv02!
But if i use echo %total% i receive Missing operator. 0
Any tips?
Here's is an example which should output the data you require using the commands you've decided are best for you!
#Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "SvcName=.Service02"
For /F Tokens^=3^ Delims^=^" %%G In ('%SystemRoot%\System32\tasklist.exe
/Fi "Services Eq %SvcName%" /Fo CSV /NH /Svc 2^>NUL') Do Set "PID=%%G"^
& For /F Tokens^=9^ Delims^=^" %%H In ('%SystemRoot%\System32\tasklist.exe
/Fi "PID Eq %%G" /Fo CSV /NH 2^>NUL') Do Set "MemKB=%%H"^
& SetLocal EnableDelayedExpansion & For /F %%I In ("!MemKB:,=!") Do EndLocal^
& Set "MemKB=%%I"
Echo %%SvcName%% = %SvcName%, %%PID%% = %PID%, %%MemKB%% = %MemKB% & Pause
You would obviously replace the last line which I've included just to show you the defined variables. I have made it simple to just replace the Service Name on line two, should you wish to do so, but nothing else should be modified at all.
BTW, I will not be explaining how any of the above works, so I advise that you perform further reasearch should you feel the need to understand it, but cannot currently do so.
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
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
I ran the following command on the windows command prompt
C:>tasklist /fi "Imagename eq BitTorrent.exe"
The output of which is
Image Name PID Session Name Session # Mem Usage
================== ======== ================= =========== =========
BitTorrent.exe 6164 Console 3 24,144K
I need to extract only one field, the PID, i.e. the number 6164 from the above output.
How do I achieve this ?
More generally, how do I extract a subset(1/more) of the fields from the output of a command on the windows command line ?
Similar to previous answers, but uses specific switches in tasklist to skip header and behave correctly irrespective of spaces in image names:
for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "imagename eq BitTorrent.exe" /fo csv') do #echo %~F
(as run directly from cmd line, if run from batch replace %F with %%F
the easiest way is with using WMIC:
c:\>wmic process where caption="BitTorrent.exe" get ProcessId
EDIT: As the WMIC is not part of home editions of windows:
for /f "tokens=1,2 delims= " %A in ('tasklist /fi ^"Imagename eq cmd.exe^" ^| find ^"cmd^"') do echo %B
Here is used CMD of the caption.You can change it in the find and tasklist parameters.
If this used in batch file you'll need %%B and %%A
You can use wmic command to not filter the output:
wmic process where name="BitTorrent.exe" get processid | MORE +1
UPDATE: Another way:
#Echo OFF
FOR /F "tokens=2" %%# in ('tasklist /fi "Imagename eq winamp.exe" ^| MORE +3') do (Echo %%#)
Pause&Exit
PS: Remember you need to set right the tokens if the app filename contain spaces.