You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Bill Burton <bi...@progress.com> on 2003/01/22 09:12:35 UTC

[SUBMIT] Support for detached on Unix and Windows

Hello,

I've written supporting scripts for running commands from <exec> 
detached in the background on most versions of Unix and Windows. 
Commands can either be run in a new window (xterm on Unix) or the output 
can be redirected to a file.

The following is from the README.txt file (included in the attachement):

This package contains scripts to support detached or asynchronous of
processes via Ant's <exec> task.  The scripts have been written in such
a way as to work cross platform (between Windows and Unix) using the 
same <exec> command.  This solution is implemented without requiring any 
changes to Ant itself.

There are two basic modes for using these scripts:
1. Detached sending output to a specified file.
2. Detached in a new window.

If no output file is specified, the command is executed in a new window.
On Unix, xterm is used to invoke the command.  A title for the window 
can be specified by setting the environment variable ANTRUN_TITLE.

When an output file is specified with the environment variable 
ANTRUN_OUTPUT, all output is redirected to to the specified file.  On 
Unix, there is no visible output to the console and if the ANTRUN_NOHUP 
environment variable is set to yes/true/nohup, the command will be 
prefixed with "nohup" allowing it to continue running after the user 
logs out.   On Windows, the command is always opened in a new window 
which may or may not be minimized depending on the version of Windows 
Scripting Host installed.  Note that if a relative filename or path is 
specfied, it will be relative to the dir attribute of the <exec> task if 
one has been specified.

Requirements:
* Ant 1.4 or later as the vmlauncher attribute to <exec> is required.
* Windows: 98/Me/2000/XP
   For Windows 95/NT 4.0, Windows Scripting Host must be installed.  It
   can be downloaded from http://msdn.microsoft.com/scripting/.
* Unix: xterm must be in the PATH if output isn't being redirected to
   a file.

MacOS X, NetWare, etc. are not currently supported as I have no way to 
test on those OS's.

Installing:
* Copy the contents of the bin subdirectory to ANT_HOME/bin.
* On Unix, ensure xterm is in the PATH if the command is being executed
   in a new window.
* Test by running:
   ant exec_window
     and
   ant exec_output

Examples:

See the included build.xml.

The <exec> task needs the location of the antRunAsync script as that is 
what it's actually executing.  Specify the location in the build.xml 
assuming the scripts have been copied to ANT_HOME/bin:

   <!-- Generic name and location of antRunAsync script -->
   <property name="antRunAsync" location="${ant.home}/bin/antRunAsync" />

Note that the suffix ".bat" must not be specified for this to work in a 
cross platform manner.

* Execute command in a new window:
   <target name="exec_window"
           description="Asynchronous Exec in a New Window">
     <exec executable="${antRunAsync}" dir="${exec_dir}"
           vmlauncher="false" failonerror="true">
       <env key="ANTRUN_TITLE" value="Background Test Script" />
       <arg line="${exec}" />  <!-- actual executable and arguments -->
     </exec>
     <sleep seconds="2" />
     <echo message="leaving target" />
   </target>

* Execute command redirecting output:
   <!-- Location of output file.  If the dir attribute is specified, the
        location is relative to that directory unless an absolute path is
        specified. -->
   <property name="exec_log" location="exec.log" />

   <target name="exec_output"
           description="Asynchronous Exec Redirecting Output">
     <exec executable="${antRunAsync}" dir="${exec_dir}"
           vmlauncher="false" failonerror="true">
       <env key="ANTRUN_OUTPUT" value="${exec_log}" />
       <!-- On Unix, prevent a logout from killing the process -->
       <env key="ANTRUN_NOHUP" value="true" />
       <arg line="${exec}" />  <!-- actual executable and arguments -->
     </exec>
     <echo message="leaving target" />
   </target>

Suggested enhancements:
* Enhance the Unix antRunAsync script so it can invoke the specified
   command in any of xterm, dterm, KDE konsole, gnome_term, screen, etc.
   on Unix.
* Write a task wrapper with appropriate nested attributes to replace the
   ANTRUN_xxx environment variables.  Such a task could also do more
   intelligent things depending on the current OS.  The antRunAsync.bat
   script could then be eliminated as the task could invoke cscript.exe
   directly with the appropriate options.
* The <exec> task could be modified support these scripts natively when
   an option such as detach="true" is specified.
* Enhance the <java> task so detach="true" can be specified with the
   same effect at the <exec> task.  This would also imply fork="true".

Comments appreciated.

-Bill
-- 
Bill Burton <bi...@progress.com>