Find out when a process has stopped

May 30, 2008
235
2
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,489
15
Solon, OH, USA
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
> ---------
>
>
>
 
May 20, 2008
11,300
95
Syracuse, NY, USA
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
 
May 20, 2008
11,300
95
Syracuse, NY, USA
There already exists @ISPROC[PID].

@ISPROC[pid] : Returns 1 if the specified process ID is an active process, or 0 if it is not.
 

samintz

Scott Mintz
May 20, 2008
1,489
15
Solon, OH, USA
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
235
2
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.
 
Similar threads
Thread starter Title Forum Replies Date
J ffind does not find files Support 4
Alpengreis List command: text truncated in find box (german) Support 9
R FIND command error Support 9
vefatica Find Files/Text dialog: does it work? Support 0
T FFind - can we display n number of lines after the find? Support 2
R How to? Find a specific file in subfolders Support 11
Patulus TCHistoryLog: TCC: (Sys) The system cannot find the path specified Support 15
D unzip fails to find valid license Support 3
R Find in screen buffer - find next behavior Support 0
thedave Problem chaining piped FIND commands in BTM Support 7
D CD doesn't always find everything Support 8
B Fixed FFIND doesn't find last character in file without CR/LF at EOF Support 1
M Fixed Cannot use the "Browse..." function in "Find files/text" dialogbox Support 2
vefatica FFIND /S, find directory with specified name? Support 7
D Why doesn't ffind find directory? Support 3
nikbackm How to? Find duplicate lines in text file Support 0
P OK, I can't find how to do this, but I know you wizards will... Support 5
noahcoad WAD cmd.exe under tcc doesn't find apps Support 4
S How to? Find installer downloaded by "option /u"? Support 11
M How to? Find out the total allocated size of a directory tree... Support 7
G Find files but ignore file extension in search Support 2
S CDD Doesn't Find Directory? Support 3
L File association: System cannot find the associated program Support 30
R How to use ffind to find older files? Support 9
Charles Dye Edit / Find dialog Support 0
J Find Files - changing folder Support 1
BobK Error from START cmd, "cannot find the file" Support 16
D ACTIVATE doesn't find windows Support 7
R A problem using find in list when word wrap? Support 2
J Find failure dialog box failure Support 0
J Problem with Find dialog Support 3
J Problem with %var:find=replace% syntax Support 5
S LIST and F for find Support 4
M TCC cannot find the path specified Support 4
M TCC Can't find WScript.exe where it says it is Support 0
vefatica SETP usually fails with a 32 bit process Support 4
x13 Check if process is elevated? Support 11
Alpengreis Remaining TCMD background process (v26) ... Support 19
vefatica Tmp files from in-process pipes not deleted Support 6
Jay Sage TASKLIST Command Process Count Support 5
Joe Caverly PSHELL and out-of-process servers Support 2
CWBillow Upgrade process Support 7
Joe Caverly SETP and 32-bit process Support 2
rps V20 In-process pipes problem Support 8
R Fixed Internal ZIP command fails to process multiple files Support 3
C Determine process svchost is running Support 13
fpefpe WAD Python support does not process command line Support 14
M WAD tcmd.exe crashes and leaves an orphaned tcc.exe process Support 6
Stefano Piccardi replace tcc process space? Support 8
Q TCC: (Sys) The process cannot access the file because it is being used by another process. Support 0

Similar threads