Why does this close immediately ? is it becouse of ERROR?
I want it to kill umcs when it can
in cmd it terminates process than prompts
ERROR: The process "UCMS.exe" not found multiple times and again terminates process
for /L %i in (1,0,2) do #ping -n 2 127.0.0.1 >nul & taskkill /F /im "UCMS.exe"
Due to the bizarre nature of the batch parser, in batch files the for loop variable must be specified with two percent signs, like this:
for /L %%i in (1,0,2) do #ping -n 2 127.0.0.1 >nul & taskkill /F /im "UCMS.exe"
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
I am unable to call a bat process within another bat.
This is the situation.
I have a n1.bat that basically contains:
TITLE "n1.bat"
...
...
start /b /MIN "n2.bat"
...
The n2.bat contains:
TITLE "n2.bat"
...
...
start someother.bat
start another.bat
exit
Now I use a third bat (n3.bat) that must kill everything.
TITLE "n3.bat"
...
TASKKILL /F /T /PID n1_PID
...
exit 0
Unfortunately when n3.bat ends a window called n1.bat - n2.bat remains active (the echo output belongs to n2.bat).
I tried to kill it in some ways:
1- Tried to get the process pid from tasklist -> there is no process called n2.bat or similar
TASKLIST /V /NH>Tasks.txt
FOR /F "tokens=2 delims= " %%n2_pid IN ('FINDSTR n2.bat Tasks.txt') DO SET PID=%%n2_pid
TASKKILL /PID %n2_pid%
2- Trying to use TASKKILL /F /FI "WINDOWTITLE eq n2.bat" -> no process found
I tried the previous solutions also starting n2.bat with
start /min "n2.bat" (so without /b)
with no success.
The only way I manage to kill it is to calling
TASKKILL /F /IM cmd.exe
that I really would like to avoid since it obviously kills all opened cmd.
Any ideas to retrieve the guilty pid?
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.
I have a X.exe program that takes about 2-6 hours to finish. Exact time is unknown, but I'd like to implement a threshold of 6.5 or 7 hours. If this program does not return any value by this amount of time, it will be killed. How do I implement this using batch *.bat files?
Here is what I had so far: a timer bat1.bat and an actual bat2.bat.
bat1.bat:
start cmd /C bat2.bat & timeout /t 25200 & taskkill /im X.exe /f
bat2.bat:
cd blah
bat1.bat
The problem with this approach is that only after 25200 seconds (or 7 hours) the timer will be stopped, and it won't be terminated before that limit. How do I tell the computer that if the program X.exe is finished then don't wait anymore?
Any help is appreciated!
I think this is a much simpler solution:
rem Start the process that will kill the X.exe program after 7 hours
start "WaitingToKill" cmd /C timeout /t 25200 ^& taskkill /im X.exe /f
rem Run the X.exe program and wait for it to terminate
X.exe
rem Kill the killer process and terminate
taskkill /fi "WINDOWTITLE eq WaitingToKill" /f
In this method there is not any additional code running at same time; just the waiting state of timeout command.
EDIT: Some explanations added
Note that both the "WaitingToKill" cmd.exe process with the timeout command and the X.exe program are running in parallel. If the timeout command ends after 7 hours, the taskkill /im X.exe /f command is executed, the X.exe program is killed and both cmd.exe processes ends.
If the X.exe program ends before the 7 hours, the Batch file execute the next line as usual. This line is taskkill /fi "WINDOWTITLE eq WaitingToKill" /f, so the window with the timeout command is killed and both cmd.exe processes ends.
thanks to #Squashman i was able to build a script on my own. seem to work fine
#echo off
setlocal enableextensions enabledelayedexpansion
set /a "checktime=60"
set /a "elapsedtime=0"
set /a "killtime=150"
set XProg=X.exe
start cmd /C runTest.bat
timeout /t 10
:while1
echo Go to WHILE loop.
echo elapsedtime = %elapsedtime%
echo killtime = %killtime%
set /a "timeleft = %killtime% - %elapsedtime%"
echo timeleft = %timeleft%
if /i %timeleft% geq 0 (
tasklist /fi "imagename eq %XProg%" 2>NUL | find /i /n "%XProg%">NUL
if "%ERRORLEVEL%"=="0" (
echo %XProg% is still running...
) else (
echo %XProg% is finished before timer.
)
set /a "elapsedtime = elapsedtime + checktime"
timeout /t %checktime%
goto :while1
) else (
taskkill /im %XProg% /f
echo %XProg% is terminated.
)
lessons learned:
1. hard to compare numeric variables in batch (compare diff with 0 instead)
2. terminated first time elaspedtime > killtime (might be a bit longer than killtime depending how often it checks)
I've tried various solutions but in the end this is really cumbersome in batch.
If you are willing to use an external tool the simplest way is
"C:\Program Files\Git\usr\bin\timeout.exe" 5 .\test.exe
It properly returns the exit code of the test process, also works if you spawn multiple test processes simultaneously and does not pop up windows all the time.
I am trying to get a song to play in the background of Windows, after a little looking I found this:
#echo off
set file=RRLJ.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
This works well for starting the song but I have no way of stopping it from a .bat file. The only way I found to cut it short is to open task manager and close it from the processes.
I have tried:
taskkill /im wscript.exe
But I keep getting something in the window saying:
Success: Sent termination sidnal to the process "wscript.exe" with PID 185448
but the music continues to play until I manually end it with task manager
I'd use /T switch (Tree kill): terminates the specified process and any child processes which were started by it.
Here is a script to find ProcessID to terminate exactly needed process only using PID:
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Note taskkill command is echoed merely... Remove echo when debugged.
This work for me, and is only one line.
Taskkill /IM "wscript.exe" /F>nul 2>&1