Find out when a process has stopped

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
May 30, 2008
205
1
#1
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.

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
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#2
Tasklist /o |! ffind /t"%curpid " /km

will list the task entry that matches the pid.

Code:
Function IsRunning=`%@if["%@execstr[tasklist /o |! ffind /t"%1 " /km]"=="",0,1]`
echo %@IsRunning[277]
0

echo %@IsRunning[2772]
1

-Scott


nikbackm <> wrote on 06/09/2009 05:12:54 AM:


> 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.
>
>
> 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
> ---------
>
>
>
 
#3
On Tue, 09 Jun 2009 04:13:06 -0500, nikbackm <> wrote:

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

Perhaps overloading "IF ISAPP" to accept PIDs as well as filenames ... or a new
"IF ISPROC PID"

For now, @MODULE from my 4UTILS plugin seems to work:

v:\> echo %_pid
548

v:\> if isapp %@module[548] echo yes
yes

ftp://lucky.syr.edu/4plugins/4utils.zip
--
- Vince
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#5
I vaguely recall Rex adding that after the last time we had one of these
PID discussions...

And I specifically looked through the help for it and completely missed it
too.

-Scott

vefatica <> wrote on 06/09/2009 12:35:55 PM:


> There already exists @ISPROC[PID].
>
> *@ISPROC[*pid*]* : Returns 1 if the specified process ID is an
> active process, or 0 if it is not.
>
>
>
>
 
May 30, 2008
205
1
#6
Tasklist /o |! ffind /t"%curpid " /km

will list the task entry that matches the pid.
Thanks, that was a good idea, sorting the tasklist avoids the "one pid being a substring of another" problem.

There already exists @ISPROC[PID].

@ISPROC[pid] : Returns 1 if the specified process ID is an active process, or 0 if it is not.
But this is better, don't know why I missed this one. Thanks!

I vaguely recall Rex adding that after the last time we had one of these
PID discussions...

And I specifically looked through the help for it and completely missed it
too.

-Scott
I looked in the help first too and missed it as well. My eyes must have just glazed over it. It would perhaps be good to have @isproc[] linked to in the DETACH and/or _DETACHPID help topics.