Get just PID in tasklist

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Get just PID in tasklist

Post by MigrationUser »

28 Feb 2013 22:40
brno

Hi

What is best way to get a PID from task and execute a command and the PID.

Example

i need run test.exe (PID OF TASK), but if PID change i have to run test.exe all the time when PID Change. What i tried was to make a BAT:

Code: Select all

tasklist > a.txt
FINDSTR "taskname.exe" a.txt > b.txt
but this give me all line including taskname pid user memory use. Anyway to make something to check if PID change each x minutes, and make some kind of regex in just the PID number and execute test.exe automatically with the new PID?
Someone who can make something like this and can code some hard things contact i have some big prize to you :o

Thanks

----------------------------

#2 28 Feb 2013 23:40
npocmaka


What you with changed PID?
If PID is changed then the process has been stopped and started again...

----------------------------

#3 01 Mar 2013 00:10
brno


Yes
Thats my problem, IF process stoped and restart PID changed ,the i have to open test.exe with new PID every time the process is closed and reopened.. I need something to check if PID is the same in X minutes, and if change get the new PID and run test.exe with new PID

----------------------------

#4 01 Mar 2013 00:19
npocmaka

Code: Select all

for /f "tokens=1,2" %%P in('tasklist ^| find "test.exe"') do (
   set pid1=%%Q
)

:loop
for /f "tokens=1,2" %%P in('tasklist ^| find "test.exe"') do (
   set pid2=%%Q
)

if "%pid1%" NEQ "%pid2%" (
  set pid1=pid2
  
  *do someting with test.exe*
) 

sleep 100
goto :loop
but this will run forever if you don't stop the console.
Post Reply