Hi,
Is there an easy way to find out if a process is still running given its process ID (PID)?
If not, perhaps there could be in v11?
It should work for processes started using DETACH (can use _DETACHPID) from a batch file.
There does not seem to be anything built-in so I coded up the following using tasklist, grep and execarray. Took a bit of experimentation to handle many of the caveats, and it's still not error proof, it would be better if it was possible to use the process handle instead of the id, though I guess Windows does not reuse them too soon.
Is there an easy way to find out if a process is still running given its process ID (PID)?
If not, perhaps there could be in v11?
It should work for processes started using DETACH (can use _DETACHPID) from a batch file.
There does not seem to be anything built-in so I coded up the following using tasklist, grep and execarray. Took a bit of experimentation to handle many of the caveats, and it's still not error proof, it would be better if it was possible to use the process handle instead of the id, though I guess Windows does not reuse them too soon.
Code:
if %debug%==1 echo Joining background task with pid "%curpid%"
set done=0
setarray atest[5]
do while %done% == 0
echo %@execarray[atest,tasklist | grep %curpid%] >& nul
REM Each element in atest will look something like: `4592 tcc `
REM We must watch out of the target pid being a substring of another unrelated pid, like 376 to 3760!
REM There might be several matching lines from the grep hence execarray instead of execstr.
REM We must assume another process with same pid has not been started in the meantime! (assuming it got done and terminated)
do itest = 0 to %@arrayinfo[atest,1]
set test="%atest[%itest]"
set testpid=%@trim[%@unquote[%@instr[0,7,%test%]]]
if %debug%==1 echo Checking: itest %itest% testpid "%testpid%" test %test%
iff "%curpid%" == "%testpid%" then
echos .
delay 1
else
set done=1
leave
endiff
enddo
enddo
unsetarray atest