You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by an...@apache.org on 2003/07/28 12:39:33 UTC

cvs commit: ant/docs/manual/CoreTasks exec.html

antoine     2003/07/28 03:39:31

  Modified:    .        WHATSNEW
               src/main/org/apache/tools/ant/taskdefs Execute.java
                        ExecTask.java
               docs/manual/CoreTasks exec.html
  Log:
  This change allows exec to start a process which will run independently of ant.
  I have used the patch prepared by Charles Hudak and Peter Nimmervoll, but made it even
  simpler, in the sense that I do not connect at all the new process to stream handlers
  and the ant logging system, disabling input, output, error, and return exec attributes
  in the case of spawn.
  Strangely, it works well on Windows if one puts a sleep of one second after having spawned
  the process. Why ? No idea.
  PR: 5907
  Submitted by: Charles Hudak ( CHudak at arrowheadgrp dot com)
  
  Revision  Changes    Path
  1.471     +3 -0      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.470
  retrieving revision 1.471
  diff -u -r1.470 -r1.471
  --- WHATSNEW	28 Jul 2003 00:22:00 -0000	1.470
  +++ WHATSNEW	28 Jul 2003 10:39:29 -0000	1.471
  @@ -521,6 +521,9 @@
   * <exec> will now work on OpenVMS (please read the notes in
     <exec>'s manual page).  Bugzilla Report 21877.
   
  +* <exec> will now have a new attribute spawn (default false).
  +If set to true, the process will be spawned. Bugzilla Report 5907.
  +
   * <parallel> now supports a timeout which can be used to recover
     from deadlocks, etc in the parallel threads. <parallel> also
     now supports a <daemons> nested element. This can be used to
  
  
  
  1.62      +36 -0     ant/src/main/org/apache/tools/ant/taskdefs/Execute.java
  
  Index: Execute.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v
  retrieving revision 1.61
  retrieving revision 1.62
  diff -u -r1.61 -r1.62
  --- Execute.java	27 Jul 2003 23:22:00 -0000	1.61
  +++ Execute.java	28 Jul 2003 10:39:30 -0000	1.62
  @@ -77,6 +77,7 @@
    *
    * @author thomas.haas@softwired-inc.com
    * @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a>
  + * @author <a href="mailto:CHudak@arrowheadgrp.com">Charles Hudak</a>
    *
    * @since Ant 1.2
    *
  @@ -103,6 +104,7 @@
       private static CommandLauncher vmLauncher = null;
       private static CommandLauncher shellLauncher = null;
       private static Vector procEnvironment = null;
  +    private boolean spawn = false;
   
       /** Used to destroy processes when the VM exits. */
       private static ProcessDestroyer processDestroyer = new ProcessDestroyer();
  @@ -171,6 +173,17 @@
           }
       }
   
  +    /**
  +     * set whether or not you want the process to be spawned
  +     * default is not spawned
  +     *
  +     * @param spawn if true you do not want ant to wait for the end of the process
  +     *
  +     * @since ant 1.6
  +     */
  +    public void setSpawn(boolean spawn) {
  +        this.spawn = spawn;
  +    }
   
       /**
        * Find the list of environment variables for this process.
  @@ -504,6 +517,29 @@
               //
               processDestroyer.remove(process);
           }
  +    }
  +
  +    /**
  +     * Starts a process defined by the command line.
  +     * Ant will not wait for this process, nor log its output
  +     *
  +     * @throws java.io.IOException The exception is thrown, if launching
  +     *            of the subprocess failed
  +     * @since ant 1.6
  +     */
  +    public void spawn() throws IOException {
  +        final Process process = launch(project, getCommandline(),
  +                                       getEnvironment(), workingDirectory,
  +                                       useVMLauncher);
  +        if (Os.isFamily("windows")) {
  +            try {
  +                Thread.sleep(1000);
  +            } catch (InterruptedException e) {
  +                project.log("interruption in the sleep after having spawned a process",
  +                    Project.MSG_VERBOSE);
  +            }
  +        }
  +        project.log("spawned process " + process.toString(), Project.MSG_VERBOSE);
       }
   
       /**
  
  
  
  1.58      +30 -13    ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
  
  Index: ExecTask.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v
  retrieving revision 1.57
  retrieving revision 1.58
  diff -u -r1.57 -r1.58
  --- ExecTask.java	27 Jul 2003 23:22:01 -0000	1.57
  +++ ExecTask.java	28 Jul 2003 10:39:30 -0000	1.58
  @@ -71,6 +71,7 @@
    * @author thomas.haas@softwired-inc.com
    * @author Stefan Bodewig
    * @author <a href="mailto:mariusz@rakiura.org">Mariusz Nowostawski</a>
  + * @author <a href="mailto:CHudak@arrowheadgrp.com">Charles Hudak</a>
    *
    * @since Ant 1.2
    *
  @@ -90,6 +91,7 @@
       private boolean failIfExecFails = true;
       private String executable;
       private boolean resolveExecutable = false;
  +    private boolean spawn = false;
   
       private Redirector redirector = new Redirector(this);
   
  @@ -100,6 +102,16 @@
       private boolean vmLauncher = true;
   
       /**
  +     * set whether or not you want the process to be spawned
  +     * default is not spawned
  +     * @param spawn if true you do not want ant to wait for the end of the process
  +     * @since ant 1.6
  +     */
  +    public void setSpawn(boolean spawn) {
  +        this.spawn = spawn;
  +    }
  +
  +    /**
        * Timeout in milliseconds after which the process will be killed.
        *
        * @param value timeout in milliseconds
  @@ -455,6 +467,7 @@
           exe.setAntRun(getProject());
           exe.setWorkingDirectory(dir);
           exe.setVMLauncher(vmLauncher);
  +        exe.setSpawn(spawn);
           String[] environment = env.getVariables();
           if (environment != null) {
               for (int i = 0; i < environment.length; i++) {
  @@ -479,22 +492,26 @@
       protected final void runExecute(Execute exe) throws IOException {
           int returnCode = -1; // assume the worst
   
  -        returnCode = exe.execute();
  +        if (!spawn) {
  +            returnCode = exe.execute();
   
  -        //test for and handle a forced process death
  -        if (exe.killedProcess()) {
  -            log("Timeout: killed the sub-process", Project.MSG_WARN);
  -        }
  -        maybeSetResultPropertyValue(returnCode);
  -        if (Execute.isFailure(returnCode)) {
  -            if (failOnError) {
  -                throw new BuildException(getTaskType() + " returned: "
  -                    + returnCode, getLocation());
  -            } else {
  -                log("Result: " + returnCode, Project.MSG_ERR);
  +            //test for and handle a forced process death
  +            if (exe.killedProcess()) {
  +                log("Timeout: killed the sub-process", Project.MSG_WARN);
  +            }
  +            maybeSetResultPropertyValue(returnCode);
  +            if (Execute.isFailure(returnCode)) {
  +                if (failOnError) {
  +                    throw new BuildException(getTaskType() + " returned: "
  +                        + returnCode, getLocation());
  +                } else {
  +                    log("Result: " + returnCode, Project.MSG_ERR);
  +                }
               }
  +            redirector.complete();
  +        } else {
  +            exe.spawn();
           }
  -        redirector.complete();
       }
   
       /**
  
  
  
  1.28      +9 -0      ant/docs/manual/CoreTasks/exec.html
  
  Index: exec.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/exec.html,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- exec.html	25 Jul 2003 10:06:31 -0000	1.27
  +++ exec.html	28 Jul 2003 10:39:31 -0000	1.28
  @@ -60,6 +60,15 @@
       <td align="center" valign="top">No</td>
     </tr>
     <tr>
  +    <td valign="top">spawn</td>
  +    <td valign="top">whether or not you want the command to be spawned<br/>
  +    Default is false.<br>
  +    If you spawn a command, its output will not be logged by ant.<br/>
  +    The input, output, error, and result property settings are not active when spawning a process.
  +    </td>
  +    <td align="center" valign="top">No</td>
  +  </tr>
  +  <tr>
       <td valign="top">output</td>
       <td valign="top">Name of a file to which to write the output. If the error stream 
         is not also redirected to a file or property, it will appear in this output.</td>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Antoine Levy-Lambert <an...@antbuild.com>.
Yes, the spawned process keeps running after ant is finished.
This is the case in the testcase which I wrote. The spawned process is sh
(the Bourne Shell interpreter) which sleeps for 10 seconds.
I also wrote a dummy java class which writes to some file during 10 seconds
and outlives ant.
Cheers,
Antoine
----- Original Message -----
From: "Stefan Bodewig" <bo...@apache.org>
To: <de...@ant.apache.org>
Sent: Tuesday, July 29, 2003 9:43 AM
Subject: Re: cvs commit: ant/docs/manual/CoreTasks exec.html


> On 28 Jul 2003, <an...@apache.org> wrote:
>
> >   This change allows exec to start a process which will run
> >   independently of ant.
>
> Will the spawned process keep running after Ant is finished?
>
> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Stefan Bodewig <bo...@apache.org>.
On 28 Jul 2003, <an...@apache.org> wrote:

>   This change allows exec to start a process which will run
>   independently of ant.

Will the spawned process keep running after Ant is finished?

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 29 Jul 2003, Steve Loughran <st...@iseran.com> wrote:

> Maybe the trick is to start a java spawner program (ours), that sets
> up stdio, does the pumping, etc, etc.

Probably.

It would have to be forked and to be save invoked via nohup on Unix
systems, though.  So there still would be some platform specific code
left.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Steve Loughran <st...@iseran.com>.
Stefan Bodewig wrote:

> On Tue, 29 Jul 2003, Antoine Levy-Lambert <an...@antbuild.com>
> wrote:
> 
> 
>>I do not know at all what is going to happen with output, error,
>>stdin for the spawned process.
> 
> 
> My guess is they cause trouble if the spawned process wants to use
> them.  I should back that claim, maybe later today/tomorrow.
> 
> 
>>I do not know how you implement OS specific redirections ?
> 
> 
> I don't know either, I'd only know for Unix.

There is NUL is DOS systems.

Maybe the trick is to start a java spawner program (ours), that sets up 
stdio, does the pumping, etc, etc.



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 29 Jul 2003, Antoine Levy-Lambert <an...@antbuild.com>
wrote:

> I do not know at all what is going to happen with output, error,
> stdin for the spawned process.

My guess is they cause trouble if the spawned process wants to use
them.  I should back that claim, maybe later today/tomorrow.

> I do not know how you implement OS specific redirections ?

I don't know either, I'd only know for Unix.

> I designed this to start something which is going to outlive ant,

Sure, I'm trying to improve upon it, that's all.

Now that you've made <exec> throw exceptions if the user combines
output with spawn (and it will be documented that way), we won't have
the user expect something wrong.  Now the user knows that she has to
tale care of the standard streams.

> I thought anyway that you do not want the output or error messages
> coming from the spawned process to pollute asynchronously the ant
> logs.

Certainly not, but you may want them to go to a file to review that
later or send them to /dev/null if you can't suppress output.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Antoine Levy-Lambert <an...@antbuild.com>.
----- Original Message -----
From: "Stefan Bodewig" <bo...@apache.org>
Sent: Tuesday, July 29, 2003 9:42 AM


> On 28 Jul 2003, <an...@apache.org> wrote:
>
> >   in the sense that I do not connect at all the new process to
> >   stream handlers and the ant logging system, disabling input,
> >   output, error, and return exec attributes in the case of spawn.
>
> Won't this cause
>
> (1) the spawned process to hang as soon as it tries to read from stdin
> (no big deal as it isn't supposed to do so, I guess).
>
> (2) the spawned process to be stoped or even killed by the system (at
> least Unix) as soon as it tries to write to stdout or stderr (probably
> either with SIGTTOU, SIGPIPE or SIGHUP).
>
> The better approach for (2) would probably be to enforce the output
> and or error attributes to be set for spawn="true" and probably use OS
> specific redirections instead of stream handlers to ensure the spawned
> process will never try to write to stdout/err.
>
I do not know at all what is going to happen with output, error, stdin for
the spawned process. I do not know how you implement OS specific
redirections ?

I designed this to start something which is going to outlive ant, I thought
anyway that you do not want the output or error messages coming from the
spawned process to pollute asynchronously the ant logs.

Antoine



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Antoine Levy-Lambert <an...@antbuild.com>.

> On Tue, 29 Jul 2003, Conor MacNeill <co...@cortexebusiness.com.au>
> wrote:
>
> >> (2) the spawned process to be stoped or even killed by the system
> >> (at least Unix) as soon as it tries to write to stdout or stderr
> >> (probably either with SIGTTOU, SIGPIPE or SIGHUP).
> >
> > Why not just "caveat user" :-)
>
> AFAIU the current implementation will ignore the error/output
> attributes, which means that the user may think everything is fine
> even if it isn't.
>
We can add a throw new BuildException in case one of the attributes which
are not supported for spawn is used together with spawn (all the
input/output/error/result properties).
This is the simplest way to handle it.

Antoine



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 29 Jul 2003, Conor MacNeill <co...@cortexebusiness.com.au>
wrote:

>> (2) the spawned process to be stoped or even killed by the system
>> (at least Unix) as soon as it tries to write to stdout or stderr
>> (probably either with SIGTTOU, SIGPIPE or SIGHUP).
> 
> Why not just "caveat user" :-)

AFAIU the current implementation will ignore the error/output
attributes, which means that the user may think everything is fine
even if it isn't.

And then even if we use our PumpStreamHandler for stdout/stderr things
are going to fail if the process is expected to outlive Ant (I'm still
not sure if it is).

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Conor MacNeill <co...@cortexebusiness.com.au>.
On Tue, 29 Jul 2003 05:42 pm, Stefan Bodewig wrote:
> On 28 Jul 2003, <an...@apache.org> wrote:
> >   in the sense that I do not connect at all the new process to
> >   stream handlers and the ant logging system, disabling input,
> >   output, error, and return exec attributes in the case of spawn.
>
> Won't this cause
>
> (1) the spawned process to hang as soon as it tries to read from stdin
> (no big deal as it isn't supposed to do so, I guess).
>

Just off the top of my head, I think we can close that stream which will cause 
an EOF rather than a hang.

> (2) the spawned process to be stoped or even killed by the system (at
> least Unix) as soon as it tries to write to stdout or stderr (probably
> either with SIGTTOU, SIGPIPE or SIGHUP).
>
> The better approach for (2) would probably be to enforce the output
> and or error attributes to be set for spawn="true" and probably use OS
> specific redirections instead of stream handlers to ensure the spawned
> process will never try to write to stdout/err.
>

Why not just "caveat user" :-)

Conor


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: cvs commit: ant/docs/manual/CoreTasks exec.html

Posted by Stefan Bodewig <bo...@apache.org>.
On 28 Jul 2003, <an...@apache.org> wrote:

>   in the sense that I do not connect at all the new process to
>   stream handlers and the ant logging system, disabling input,
>   output, error, and return exec attributes in the case of spawn.

Won't this cause

(1) the spawned process to hang as soon as it tries to read from stdin
(no big deal as it isn't supposed to do so, I guess).

(2) the spawned process to be stoped or even killed by the system (at
least Unix) as soon as it tries to write to stdout or stderr (probably
either with SIGTTOU, SIGPIPE or SIGHUP).

The better approach for (2) would probably be to enforce the output
and or error attributes to be set for spawn="true" and probably use OS
specific redirections instead of stream handlers to ensure the spawned
process will never try to write to stdout/err.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org