How to detect that an exec'd process has finished 
Author Message
 How to detect that an exec'd process has finished

Hello,

I have the following problem that I would like to solve:

   I start a process in the background via [exec] or [open].
   How do I know it is finished?

Possibilities that I can think of are:
- It will write some progress messages to the screen. So
  I could catch these, using [open] and [fileevent] I suppose.

- If I use [exec], I will get the process ID, but can I
  use this to monitor the programs termination?

Any suggestions?

(I am working on UNIX, but no doubt my colleagues will want a
portable solution.)

Regards,

Arjen



Sat, 31 Jul 2004 21:24:23 GMT  
 How to detect that an exec'd process has finished

Quote:

> Hello,

> I have the following problem that I would like to solve:

>    I start a process in the background via [exec] or [open].
>    How do I know it is finished?

> Possibilities that I can think of are:
> - It will write some progress messages to the screen. So
>   I could catch these, using [open] and [fileevent] I suppose.

> - If I use [exec], I will get the process ID, but can I
>   use this to monitor the programs termination?

> Any suggestions?

> (I am working on UNIX, but no doubt my colleagues will want a
> portable solution.)

on UNIX you could poll via ps on your pid, but that is ugly and
platform dependent.

use open and setup a readable fileevent if the program outputs stuff
then this can provide realtime feedback & when an eof condition
occurs you know the process is complete

see http://mini.net/tcl/1757.html and
http://mini.net/tcl/2196.html for some more details/info

Also - if you have BLT the bgexec is very easy to do this kind of stuff

Bruce



Sat, 31 Jul 2004 22:51:38 GMT  
 How to detect that an exec'd process has finished
<snip>
Quote:
>    I start a process in the background via [exec] or [open].
>    How do I know it is finished?

<snip>

Can you do it through signals? i.e. the SIGCHLD tells you when your
offspring have died. If you have a handler for that, you will be
notified.

As for portability, I think process management is largely OS dependent.
Feel free to argue that.

McIan

PS If you want some really hacky schemes, involving pipes and things, I
can cook something up!



Sat, 31 Jul 2004 23:19:18 GMT  
 How to detect that an exec'd process has finished
Well, I use this (admittedlty baroque) bit:

http://mini.net/cgi-bin/wikit/2824.html

Quote:

> Hello,

> I have the following problem that I would like to solve:

>    I start a process in the background via [exec] or [open].
>    How do I know it is finished?



Sun, 01 Aug 2004 01:30:27 GMT  
 How to detect that an exec'd process has finished

Quote:

> Well, I use this (admittedlty baroque) bit:

> http://mini.net/cgi-bin/wikit/2824.html


> > Hello,

> > I have the following problem that I would like to solve:

> >    I start a process in the background via [exec] or [open].
> >    How do I know it is finished?

Thank you all, I will try the approach with [fileevent] (given
all the sample code fragments, I should be able to solve this!)

Regards,

Arjen



Mon, 02 Aug 2004 16:33:28 GMT  
 How to detect that an exec'd process has finished

Quote:

>    I start a process in the background via [exec] or [open].
>    How do I know it is finished?

Here's what I use:

proc commandOutput { fd } {
        global settings

        if { [ gets $fd output ] < 0 } {
                set settings(CMDRUNNING) [ catch { close $fd } ]
                append settings(CMDOUTPUT) \n
        } else {
                append settings(CMDOUTPUT) ${output}\n
                # If you don't want output to be output,
                # as it occurs, omit this line
                puts $output
        }

Quote:
}

proc executeCommand { cmd } {
        global settings

        set settings(CMDRUNNING) {}
        set settings(CMDOUTPUT) {}

        # Cheeky little hack to make sure both stderr and stdout
        # go to stdout. Tcl won't let me do the |& without another
        # command in the pipeline. *UNIX SPECIFIC* unless you've got
        # an implementation of tee on your platform    
        set cmdOut [ open "| $cmd |& /usr/bin/tee" r ]
        fconfigure $cmdOut -buffering line
        fileevent $cmdOut readable "commandOutput $cmdOut"

        vwait settings(CMDRUNNING)

        puts {}

        if { $settings(CMDRUNNING) } {
                lappend settings(ERRORS) $settings(CMDOUTPUT)
        }

        return $settings(CMDRUNNING)

Quote:
}

Example

% executeCommand { ps -f }
# ... output ommitted, but also in
% puts $settings(CMDOUTPUT)



Mon, 02 Aug 2004 21:05:51 GMT  
 How to detect that an exec'd process has finished

Quote:

>         # Cheeky little hack to make sure both stderr and stdout
>         # go to stdout. Tcl won't let me do the |& without another
>         # command in the pipeline. *UNIX SPECIFIC* unless you've got
>         # an implementation of tee on your platform
>         set cmdOut [ open "| $cmd |& /usr/bin/tee" r ]
>         fconfigure $cmdOut -buffering line
>         fileevent $cmdOut readable "commandOutput $cmdOut"

This code looks very clear to me. I do not need the "tee",
as the program I want to control does not write to stderr.

Regards,

Arjen



Mon, 02 Aug 2004 21:33:09 GMT  
 How to detect that an exec'd process has finished

Quote:


> >         # Cheeky little hack to make sure both stderr and stdout
> >         # go to stdout. Tcl won't let me do the |& without another
> >         # command in the pipeline. *UNIX SPECIFIC* unless you've got
> >         # an implementation of tee on your platform
> >         set cmdOut [ open "| $cmd |& /usr/bin/tee" r ]
> >         fconfigure $cmdOut -buffering line
> >         fileevent $cmdOut readable "commandOutput $cmdOut"

> This code looks very clear to me. I do not need the "tee",
> as the program I want to control does not write to stderr.

> Regards,

> Arjen

The other thing about "tee" is that it does not buffer its output.
May be that is yet another hidden useful feature.

The algorithm does do the job it seems - still some further testing
needed of course.

Regards,

Arjen



Mon, 02 Aug 2004 22:36:04 GMT  
 How to detect that an exec'd process has finished

| > >         # go to stdout. Tcl won't let me do the |& without another
| > >         # command in the pipeline. *UNIX SPECIFIC* unless you've got
| > >         # an implementation of tee on your platform
| > >         set cmdOut [ open "| $cmd |& /usr/bin/tee" r ]

- why not use

| The other thing about "tee" is that it does not buffer its output.

If the input to `tee' is buffered, `tee' can't do anything about that.
If the input to `tee' (=output of `cmd') is not buffered, calling the
`cmd' directly should not have any disadvantage.

R'



Tue, 03 Aug 2004 02:34:52 GMT  
 How to detect that an exec'd process has finished

Quote:


> | > >         # go to stdout. Tcl won't let me do the |& without another
> | > >         # command in the pipeline. *UNIX SPECIFIC* unless you've got
> | > >         # an implementation of tee on your platform
> | > >         set cmdOut [ open "| $cmd |& /usr/bin/tee" r ]

> - why not use

> | The other thing about "tee" is that it does not buffer its output.

> If the input to `tee' is buffered, `tee' can't do anything about that.
> If the input to `tee' (=output of `cmd') is not buffered, calling the
> `cmd' directly should not have any disadvantage.

> R'

I had to look up the actual use of "tee", and in the man-page I saw
that "tee" does not buffer its output. Somewhere in the back of my mind
I saw Expect's magical workings with pseudoterminals and the like again.
Hence my remark, but you are probably right. Quite right.

Anyway, it does not matter in my case: the output from my program is
buffered
and remains buffered. But I get the event loop to stop in time. And that
was
what I wanted.

Regards,

Arjen



Tue, 03 Aug 2004 15:41:04 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. waiting for an exec'ed process to finish

2. Detecting finish of parent process from child

3. How to detect a background process has finished

4. Detecting When Startup Has Finished

5. Detecting when a Mac has finished booting up

6. interaction with exec'ed process

7. VW 3.0 :After starting an NT process, can you detect when process has finished?

8. How to wait until exec is finished?

9. cgi-bin "exec": error waiting for process to exit: No child processes

10. exec, without hanging gui until finished?

11. RUN used in program, but not waiting for process to finish

12. Waiting for processes to finish under Solaris

 

 
Powered by phpBB® Forum Software