Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get PID of "exec" command in fg?

1,835 views
Skip to first unread message

Marcos

unread,
Mar 15, 2012, 8:50:01 AM3/15/12
to
Dear friends,

I'm using the exec command to run the program espeak (speech
synthesizer - http://espeak.sourceforge.net/)

Ex:

set text "A message"
exec espeak $text

When I run the program in foreground the command does not return
until the end of the command.

Only when I run in background (with &) the command return the PID of
the process.

exec espek $text &

I intend to use the PID to control the process with

kill -STOP PID
kill -CONT PID
kill PID

How do I get the PID of the exec command running espeak in foreground?

Or how to know the end of the command execution running in the
background?

Thanks for any suggestion,
Marcos

Arjen Markus

unread,
Mar 15, 2012, 10:47:31 AM3/15/12
to
Op donderdag 15 maart 2012 13:50:01 UTC+1 schreef Marcos het volgende:
You will want to use the [open] command and [fileevent] (or [chan event])
This enables you to interact with the background process.

See the examples on the Wiki.

Regards,

Arjen

Alexandre Ferrieux

unread,
Mar 15, 2012, 1:35:13 PM3/15/12
to
On Mar 15, 3:47 pm, Arjen Markus <arjen.markus...@gmail.com> wrote:
> Op donderdag 15 maart 2012 13:50:01 UTC+1 schreef Marcos het volgende:
>
>
>
>
>
>
>
>
>
> > Dear friends,
>
> > I'm using the exec command to run the program espeak (speech
> > synthesizer -http://espeak.sourceforge.net/)
>
> > Ex:
>
> > set  text  "A message"
> > exec  espeak  $text
>
> > When I run the program in foreground  the command does not return
> > until the end of the command.
>
> > Only when I run in background (with &) the command return the PID of
> > the process.
>
> > exec  espek  $text  &
>
> > I intend to use the PID to control the process with
>
> > kill -STOP PID
> > kill -CONT PID
> > kill PID
>
> > How do I get the PID of the exec command running espeak in foreground?
>
> > Or how to know the end of the command execution running in the
> > background?
>
> > Thanks for any suggestion,
> > Marcos
>
> You will want to use the [open] command and [fileevent] (or [chan event])
> This enables you to interact with the background process.
>
> See the examples on the Wiki.
>
> Regards,
>
> Arjen

Additionally, if you're just interested in the pid, it is the value
returned by [exec &].

-Alex

Erik Leunissen

unread,
Mar 15, 2012, 1:48:10 PM3/15/12
to
On 15/03/12 13:50, Marcos wrote:

-- 8< --

> How do I get the PID of the exec command running espeak in foreground?

You can't; at least not from inside Tcl.

The Tcl command [exec] returns the exit status of the invoked
executable. See also the manual page for [exec].

So, [exec espeak $sometext] returns the exit status of the invoked
espeak program. And in case that espeak doesn't finish, the call to
[exec] doesn't return, i.o.w. "it blocks".

>
> Or how to know the end of the command execution running in the
> background?

By querying the operating system for the PID of the espeak process,
which you get from the following invocation (something that you already
know, apparently):

set PID [exec $pathToEspeak $someText]

Though Tcl doesn't provide any built-in commands to query the operating
system for process information, you could invoke another program or a
command provided by a Tcl extension to retrieve that information.

On unix derived operating systems you could use "top". On windows, I
wouldn't know of a separate program that outputs such information to a
channel from which another program (like Tcl) can read. But luckily, we
there exists a Tcl extension twapi of which I'm confident that it holds
commands to provide you with that information.

By the way, if your purpose were to interact with a running espeak
program (but your question doesn't indicate that), then you'd need the
[open] command as indicated by Arjen Markus.

HTH,

Erik Leunissen.

>
> Thanks for any suggestion,
> Marcos


--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Erik Leunissen

unread,
Mar 15, 2012, 1:51:32 PM3/15/12
to
On 15/03/12 18:48, Erik Leunissen wrote:
>
> set PID [exec $pathToEspeak $someText]
>

of course that would have to be

set PID [exec $pathToEspeak $someText &]
^

Erik

Marcos

unread,
Mar 17, 2012, 10:08:26 AM3/17/12
to
On Mar 15, 11:47 am, Arjen Markus <arjen.markus...@gmail.com> wrote:
> Op donderdag 15 maart 2012 13:50:01 UTC+1 schreef Marcos het volgende:
>
>
>
> > Dear friends,
>
> > I'm using the exec command to run the program espeak (speech
> > synthesizer -http://espeak.sourceforge.net/)
>
> > Ex:
>
> > set  text  "A message"
> > exec  espeak  $text
>
> > When I run the program in foreground  the command does not return
> > until the end of the command.
>
> > Only when I run in background (with &) the command return the PID of
> > the process.
>
> > exec  espek  $text  &
>
> > I intend to use the PID to control the process with
>
> > kill -STOP PID
> > kill -CONT PID
> > kill PID
>
> > How do I get the PID of the exec command running espeak in foreground?
>
> > Or how to know the end of the command execution running in the
> > background?
>
> > Thanks for any suggestion,
> > Marcos
>
> You will want to use the [open] command and [fileevent] (or [chan event])
> This enables you to interact with the background process.
>
> See the examples on the Wiki.
>
> Regards,
>
> Arjen

Thanks for the suggestions.

I didn't know I could use the command "open" in this way.

Based on the example http://wiki.tcl.tk/4215 I did:

set executable "espeak -vpt -s100"

set chanel [open "|$executable" r+]

Then:

puts $chanel "Text to be spoken"

flush $chanel

But the flush command only returns at the end of speech processing.

So I used the command

after 0 [list command ...]

to run and espeak, inside one procedure, and vwait to leave GUI active
to pause the execution of espeak with the commands:

exec pidof espeak
9243
exec kill -STOP 9243
exec kill -CONT 9243

Thanks for all suggestions,
Marcos

Gerald W. Lester

unread,
Mar 17, 2012, 10:26:59 AM3/17/12
to
On 3/17/12 9:08 AM, Marcos wrote:
> On Mar 15, 11:47 am, Arjen Markus<arjen.markus...@gmail.com> wrote:
>...
> set executable "espeak -vpt -s100"
>
> set chanel [open "|$executable" r+]
>
> Then:
>
> puts $chanel "Text to be spoken"
>
> flush $chanel
>
> But the flush command only returns at the end of speech processing.

Use fconfigure to make the channel non-blocking then the flush will not wait.

I think you can then use fileevent writable to know when the speech
processing is done.


+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Erik Leunissen

unread,
Mar 17, 2012, 12:49:35 PM3/17/12
to
On 17/03/12 15:08, Marcos wrote:

>
> set executable "espeak -vpt -s100"
>
> set chanel [open "|$executable" r+]
>

--8<--

> to run and espeak, inside one procedure, and vwait to leave GUI active
> to pause the execution of espeak with the commands:
>
> exec pidof espeak
> 9243

Note that in the case of using [open], you can do the following to
obtain the PID of $executable:

set PID [pid $channel]

--
Erik.

> exec kill -STOP 9243
> exec kill -CONT 9243
>
> Thanks for all suggestions,
> Marcos


0 new messages