batch file PID

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

batch file PID

#1 Post by barnabe0057 » 07 Nov 2020 19:23

Hi guys,
I would like to know if there is a trick to get a .bat file PID. Apart from "converting" the file to .exe I don't know how to get the matching PID among all the cmd.exe & conhost.exe processes, that would be very useful to me.
Thank you in advance.

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: batch file PID

#2 Post by T3RRY » 08 Nov 2020 03:15

It becomes Very easy if you use a title to flag your script.

Code: Select all

@Echo off
Set /A UID=%random% %% 10000
Title %~n0_%UID%
2> nul (For /F "tokens=2 Delims=: " %%G in ( 'Tasklist /Fi "windowtitle eq %~n0_%UID%" /Fi "imagename eq cmd.exe" /FO:List ^| Findstr.exe /LIC:"PID"' )Do Set "%~n0_%UID%=%%G")
Set %~n0_%UID%
The randomised UID isn't strictly necessary, however simplifies isolation of a given instance of the script should multiple instances be running.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: batch file PID

#3 Post by dbenham » 08 Nov 2020 06:07

T3RRY wrote:
08 Nov 2020 03:15
The randomised UID isn't strictly necessary, however simplifies isolation of a given instance of the script should multiple instances be running.
Ensuring the titles differ is necessary if there is a chance the script may be run in multiple instances at the same time. Unfortunately %RANDOM% is not good enough because two CMD sessions that are started within the same second will always get the same random number - the cmd.exe seed for the random number generator is derived from the time.

Years ago I wrote a bullet proof solution for a batch file to determine its own PID: viewtopic.php?p=38870#p38870. It uses a lock file to guarantee there is no confusion between two processes started at the same time.


Dave Benham

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: batch file PID

#4 Post by T3RRY » 08 Nov 2020 07:34

dbenham wrote:
08 Nov 2020 06:07
T3RRY wrote:
08 Nov 2020 03:15
The randomised UID isn't strictly necessary, however simplifies isolation of a given instance of the script should multiple instances be running.
Ensuring the titles differ is necessary if there is a chance the script may be run in multiple instances at the same time. Unfortunately %RANDOM% is not good enough because two CMD sessions that are started within the same second will always get the same random number - the cmd.exe seed for the random number generator is derived from the time.

Years ago I wrote a bullet proof solution for a batch file to determine its own PID: viewtopic.php?p=38870#p38870. It uses a lock file to guarantee there is no confusion between two processes started at the same time.


Dave Benham
Very robust indeed.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: batch file PID

#5 Post by npocmaka_ » 08 Nov 2020 09:13

https://github.com/npocmaka/batch.scrip ... CmdPID.bat - you can try with this. It saves the PID to the errorlevel

barnabe0057
Posts: 21
Joined: 04 Aug 2017 14:20
Location: France

Re: batch file PID

#6 Post by barnabe0057 » 08 Nov 2020 13:34

My problem is solved thanks to you.
Thank you all for your answers, have a good week.

Eureka!
Posts: 136
Joined: 25 Jul 2019 18:25

Re: batch file PID

#7 Post by Eureka! » 08 Nov 2020 15:12

Alternative:

Code: Select all

for /f "tokens=2 delims==" %%i in (
  'wmic process WHERE ^"CommandLine LIKE '%comspec:\=\\% /c wmic process WHERE \^"CommandLine LIKE%%'^" get ParentProcessId /value'
) do set "PID=%%i"
(based on this thread )

EDIT:
Never mind; a similar solution was already mentioned in one of the linked threads ....

Post Reply