138701
May 05, 2019, updated at 12:40:23 (UTC)
2748
3
0
Eigene PID feststellen
Hallo zusammen,
Ich habe 2 Programme:
- menu.bat (Wird vom Nutzer gestartet)
- start.bat (Wird von start.vbs gestartet)
menu.bat wird im Vordergrund gestartet, öffnet jedoch beim Start die Datei "start.vbs"
start.vbs
Wie bekomme ich es hin, dass wenn menu.bat beendet wird, start.bat automatisch ebenfalls beendet wird?
--> Wie kann ein Batch-Script seine eigene PID erfassen, selbst wenn mehrere cmd.exe Programme ausgeführt werden?
Ich habe 2 Programme:
- menu.bat (Wird vom Nutzer gestartet)
- start.bat (Wird von start.vbs gestartet)
menu.bat wird im Vordergrund gestartet, öffnet jedoch beim Start die Datei "start.vbs"
start.vbs
set fs=createobject("wscript.shell")
fs.run "start.bat", 0
Wie bekomme ich es hin, dass wenn menu.bat beendet wird, start.bat automatisch ebenfalls beendet wird?
--> Wie kann ein Batch-Script seine eigene PID erfassen, selbst wenn mehrere cmd.exe Programme ausgeführt werden?
Please also mark the comments that contributed to the solution of the article
Content-Key: 448012
Url: https://administrator.de/contentid/448012
Printed on: April 20, 2024 at 09:04 o'clock
3 Comments
Latest comment
Moin,
versuche es mal hiermit:
Klappt natürlich nur eindeutig, wenn eine Batch mit exakt diesem Titel nur einmal zur selben Zeit läuft...
Gruß
em-pie
versuche es mal hiermit:
@Echo off & SetLocal EnableDelayedExpansion
Set cmdTitle=myCMD
TITLE %cmdTitle%
set "PID="
for /f "tokens=2" %%A in ('tasklist /v ^| findstr /i "%cmdTitle%" 2^>NUL') do @Set "PID=!PID!,%%A"
Echo "your PID:" %PID:~1%
Pause
Klappt natürlich nur eindeutig, wenn eine Batch mit exakt diesem Titel nur einmal zur selben Zeit läuft...
Gruß
em-pie
Hallo,
hier noch eine reine Batch-Lösung, die die Kommandozeile aller CMD-Prozesse darauf prüft, ob
Der Filterteil
dient dazu, die unsichtbare CMD-Instanz zu ignorieren, die den Befehl in der Klammer der
Informationen zum
Informationen zu WQL (WMI Query Language): https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wql-sql-for-wmi
Grüße
Friemler
hier noch eine reine Batch-Lösung, die die Kommandozeile aller CMD-Prozesse darauf prüft, ob
start.bat
darin enthalten ist:@echo off & setlocal
set "BatchFileName=start.bat"
for /f "tokens=2 delims==" %%a in ('wmic Process where "Name='cmd.exe' and (CommandLine like '%% %BatchFileName%%%' or CommandLine like '%%\\%BatchFileName%%%') and not CommandLine like '%%cmd.exe /c wmic Process %%'" get ProcessId /format:list 2^>NUL') do (
for /f %%b in ('set /p "=%%a" ^<NUL') do (
taskkill /pid %%b > NUL
)
)
Der Filterteil
and not CommandLine like '%%cmd.exe /c wmic Process %%'
FOR
-Schleife ausführt, denn diese CMD-Instanz hat natürlich ebenfalls eine Kommandozeile, in der start.bat
vorkommt. Informationen zum
WMIC
-Befehl: https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wmicInformationen zu WQL (WMI Query Language): https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wql-sql-for-wmi
Grüße
Friemler