This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: fork-exec *nix process from scala

2 replies
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Sam,

Thanks for your thoughts. i need to know when the process is finished. The trouble is the process is finished nearly instantaneously; but whatever thread is waiting on the waitFor is blocked indefinitely. i was merely illustrating this difficulty with the scala repl.

Best wishes,

--greg

On Sun, Jan 4, 2009 at 11:22 AM, Samuel Robert Reid <Reids [at] colorado [dot] edu> wrote:
The process is forked when you exec().  When you call waitFor(), this is causing the blocking (like a join()).  You could skip calling waitFor, or do the exec+waitFor in a new Thread (or with an Actor) if you need to know when it's finished.

Sam Reid

Meredith Gregory wrote:
Scalads and Lasses,

Anybody have any tips for exec'ing a *nix process from scala? The following blocks indefinitely.

Best wishes,

--greg

scala> Runtime.getRuntime().exec( "dot -Tsvg phred.dot -o phred" + (System.currentTimeMillis) + ".svg" )
res1: java.lang.Process = java.lang.UNIXProcess@1e0b6a
scala> res1.waitFor
C-c C-cres4: Int = 0

scala> bash-3.2$


extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: fork-exec *nix process from scala

On Sun, Jan 04, 2009 at 11:26:13AM -0800, Meredith Gregory wrote:
> Thanks for your thoughts. i need to know when the process is finished. The
> trouble is the process is finished nearly instantaneously; but whatever
> thread is waiting on the waitFor is blocked indefinitely. i was merely
> illustrating this difficulty with the scala repl.

Are you sure the process is finished? It works for me (I'm on osx.)

$ cat ./foo
#!/bin/bash
#

sleep 5
echo "done"

$ scala27
Welcome to Scala version 2.7.2.final (Java HotSpot(TM) Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Runtime.getRuntime().exec("./foo")
res0: java.lang.Process = java.lang.UNIXProcess@7e5e84

scala> res0.waitFor
[5 seconds pass]
res1: Int = 0

ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: fork-exec *nix process from scala

Meredith Gregory wrote:
> Sam,
>
> Thanks for your thoughts. i need to know when the process is finished.
> The trouble is the process is finished nearly instantaneously; but
> whatever thread is waiting on the waitFor is blocked indefinitely. i was
> merely illustrating this difficulty with the scala repl.

You might want to close the stream going to the child process' stdin,
and start other threads to consume and close the streams for stdout /
stderr.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland