Frage

For reasons that have yet to be determined, the /b switch in start /b causes taskkill to fail to find the first instance:

INFO: No tasks running with the specified criteria

This is a simple example of the code in question (has /b and does not work). Copy the code and paste in notepad and save as batch file. It will restart with the cmd switch "/k", then it will create a second instance, then the second instance will attempt to end the first. If needed, original script can be found here.

@prompt :
%zero%@set zero=::&start /b "" "cmd" /k "%~dpnx0"&goto :eof

%first%@TITLE First Instance
%first%@set first=::&start "" cmd /C "%~dpnx0"&goto :waiting

taskkill /F /FI "WINDOWTITLE eq First Instance"
taskkill /F /FI "WINDOWTITLE eq Administrator:  First Instance"

:waiting
pause>nul

Here is the same example without the /b switch. That is the only difference between the two. It works as it should:

@prompt :
%zero%@set zero=::&start "" "cmd" /k "%~dpnx0"&goto :eof

%first%@TITLE First Instance
%first%@set first=::&start "" cmd /C "%~dpnx0"&goto :waiting

taskkill /F /FI "WINDOWTITLE eq First Instance"
taskkill /F /FI "WINDOWTITLE eq Administrator:  First Instance"

:waiting
pause>nul

To reiterate my question: how can one get taskkill to work with the /b switch of start? An explanation of why it doesn't work as it is would be nice as well.

War es hilfreich?

Lösung

@ECHO OFF
SETLOCAL
@prompt :
%zero%@set zero=::&start "Zeroth instance" "cmd" /k "%~dpnx0"&pause&goto :eof
ECHO Maybe CHANGE TITLE...
pause
%first%@TITLE First Instance
%first%@set first=::&start "Second instance" cmd /C "%~dpnx0"&goto :waiting
ECHO got here-------------
pause
taskkill /F /FI "WINDOWTITLE eq First Instance"
taskkill /F /FI "WINDOWTITLE eq Administrator:  First Instance"

:waiting
ECHO waiting...
PAUSE
exit
GOTO :EOF

Minor changes with lots of echoes and pauses...

If you run an additional session using tasklist /v|find /i "cmd" you'll find that with the above code, the window title "Zeroth instance" will be changed to "First instance" when the appropriate pause receives a response. Allowing 'Second instanceto reach thetaskkills will close theZeroth/First` window and if either new window reaches the exit statement, that instance is terminated. All fine and good and as expected. Using the additional session, you can see the various phases entered.

If you reintroduce the /b switch, then THAT instance no longer has its own window, and hence the window that it hasn't got can't have a title. You can see this using the additional session. Now - getting that particular session to terminate is a bit of a game.... not sure start /b plays nicely with an interactive batch...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top