138701
Goto Top

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
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?

Content-Key: 448012

Url: https://administrator.de/contentid/448012

Printed on: April 20, 2024 at 09:04 o'clock

Member: em-pie
Solution em-pie May 05, 2019 at 12:49:19 (UTC)
Goto Top
Moin,

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
Member: rubberman
Solution rubberman May 05, 2019 at 15:06:47 (UTC)
Goto Top
@echo off &setlocal
set get_own_pid=powershell -nop -ep Bypass -c "exit (gwmi Win32_Process -filter \"ProcessID=$pid\").ParentProcessID"  

%get_own_pid%
echo PID: %errorlevel%

pause

Steffen
Member: Friemler
Solution Friemler May 05, 2019 at 15:32:22 (UTC)
Goto Top
Hallo,

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 %%'
dient dazu, die unsichtbare CMD-Instanz zu ignorieren, die den Befehl in der Klammer der FOR-Schleife ausführt, denn diese CMD-Instanz hat natürlich ebenfalls eine Kommandozeile, in der start.bat vorkommt. face-wink

Informationen zum WMIC-Befehl: https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wmic
Informationen zu WQL (WMI Query Language): https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/wql-sql-for-wmi

Grüße
Friemler