Page 1 of 1
Taskkill %0
Posted: 26 Apr 2021 07:52
by Arne Schönbohm
Hello,
I have a long batch that runs for some hours. To see how far it is the Title changes:
TITLE %0
...
TITLE %0 - file1
CALL C:\Path\file1
TITLE %0 - file2
CALL C:\Path\file2
TITLE %0 - file3
CALL C:\Path\file3
...
It can happen that it runs very long. Since this runs every 24h I would like to close the older turn if the next turn starts.
TASKKILL /F /FI "WINDOWTITLE EQ "%0"" would work if the i would not add "- file1" and so on. But if I add * to this command it doesn't work:
TASKKILL /F /FI "WINDOWTITLE EQ "%0" *"
Where is my mistake or does it not work this way?
Re: Taskkill %0
Posted: 26 Apr 2021 10:54
by aGerman
It's a little problematic due to the quotes around the own path of the script. I'd probably use a filtered output of tasklist to get the PID. As long as the title still doesn't contain the " - " you can use this snippet in the same script that later on calls file1-N.
Code: Select all
set me=%0
set me=%me:\=\\%
for /f tokens^=3^ delims^=^" %%i in ('"tasklist /v /nh /fi "imagename eq cmd.exe" /fo csv|findstr /c:"%me:"=\"% - ""') do (
taskkill /f /pid %%i
)
Steffen
Re: Taskkill %0
Posted: 26 Apr 2021 10:56
by Squashman
If you are scheduling this to run with Windows Task Scheduler it has this functionality built-in to the task creation. You just need edit the appropriate settings in the scheduled task.
Re: Taskkill %0
Posted: 27 Apr 2021 03:14
by Arne Schönbohm
Squashman wrote: ↑26 Apr 2021 10:56
If you are scheduling this to run with Windows Task Scheduler it has this functionality built-in to the task creation. You just need edit the appropriate settings in the scheduled task.
The batch is not scheduled with Windows Task Scheduler but still thanks for your reply.
Re: Taskkill %0
Posted: 27 Apr 2021 03:15
by Arne Schönbohm
aGerman wrote: ↑26 Apr 2021 10:54
It's a little problematic due to the quotes around the own path of the script. I'd probably use a filtered output of tasklist to get the PID. As long as the title still doesn't contain the " - " you can use this snippet in the same script that later on calls file1-N.
Code: Select all
set me=%0
set me=%me:\=\\%
for /f tokens^=3^ delims^=^" %%i in ('"tasklist /v /nh /fi "imagename eq cmd.exe" /fo csv|findstr /c:"%me:"=\"% - ""') do (
taskkill /f /pid %%i
)
Steffen
Wow thank you. This works like a charm.
Re: Taskkill %0
Posted: 27 Apr 2021 06:14
by Hackoo
Hi
You can also use the
WMIC to "kill your self" oh no
I mean killing your batch file
Code: Select all
@echo off
Title BATCH SELF KILLING
:MAIN
CLS
Set "ME=%~0"
Set "ME=%ME:\=\\%"
REM Just To show what can be the result
wmic PROCESS where "CommandLine Like '%%%ME%%%' And Name Like '%%cmd.exe%%'" get ProcessID,Name,CommandLine
::----------------------------------------------------------------------------------------------------------------------------------------------
@for /f "Tokens=2 Delims==" %%a in ('wmic PROCESS where "CommandLine Like '%%%ME%%%' And Name Like '%%cmd.exe%%'" get ProcessID /Value') do (
@for /f "delims=" %%b in ("%%a") do (
set "MyPID=%%~nb"
)
)
::----------------------------------------------------------------------------------------------------------------------------------------------
echo MyPID = "%MyPID%"
echo Do you want to Kill your Self ? oh No Sorry (-_°) I mean killing the current batch file [Y=YES]
Set /P "Answer="
IF /I [%Answer%] EQU [Y] Taskkill /f /pid %MyPID%
) Else (
echo Thanks to God ... We are still alive hhhhh
TimeOut /T 3 /Nobreak>nul
IPconfig /ALL
)
pause>nul & GOTO Main
Re: Taskkill %0
Posted: 27 Apr 2021 10:10
by Compo
Arne Schönbohm wrote: ↑26 Apr 2021 07:52
TASKKILL /F /FI "WINDOWTITLE EQ "%0"" would work if the i would not add "- file1" and so on. But if I add * to this command it doesn't work:
TASKKILL /F /FI "WINDOWTITLE EQ "%0" *"
Where is my mistake or does it not work this way?
Untested, but have you tried including the wildcard within the doublequoted filter itself?
Code: Select all
%SystemRoot%\System32\taskkill.exe /F /Fi "WindowTitle Eq %0*"
It may also be quicker to filter the image name first too:
Code: Select all
%SystemRoot%\System32\taskkill.exe /F /Fi "ImageName Eq cmd.exe" /Fi "WindowTitle Eq %0*"
I will add however, that '%0' used like that is probably not a wise idea if the batch file name it represents contains spaces or other poison characters, as it could itself be doublequoted. That would obviously case an issue with the doublequotes in your filter. For that reason it would be better were you to not use it like that in your title. What about using '%~nx0' or '%~s0', if you really need that in the title, although it may be better to just use a known safe string instead.
I do not however recommend that you use the '/F' option to force close your task, allow it to close properly, and omit the '/F' option