On Unix, you can use a wrapper like this to make the daemon behave.
For example:
daemonize -E BUILD_ID=dontKillMe /path/to/your/command
In a Jenkins Pipeline, use
JENKINS_NODE_COOKIE
instead of
BUILD_ID
.
Note that this will set the BUILD_ID environment variable for the process being spawned to something other than the current BUILD_ID.
Or you can start jenkins with
-Dhudson.util.ProcessTree.disable=true
- see long running agent process for details.
On Windows, use the 'at' command to launch a process in the background.
For example:
<scriptdef name="get-next-minute" language="beanshell">
<attribute name="property" />
date = new java.text.SimpleDateFormat("HH:mm")
.format(new Date(System.currentTimeMillis() + 60000));
project.setProperty(attributes.get("property"), date);
</scriptdef>
<get-next-minute property="next-minute" />
<exec executable="at">
<arg value="${next-minute}" />
<arg value="/interactive" />
<arg value="${jboss.home}\bin\run.bat" />
</exec>
Another similar workaround on Windows is to use a wrapper script and launch your program through it:
// antRunAsync.js - Wrapper script to run an executable detached in the
// background from Ant's <exec> task. This works by running the executable
// using the Windows Scripting Host WshShell.Run method which doesn't copy
// the standard filehandles stdin, stdout and stderr. Ant finds them closed
// and doesn't wait for the program to exit.
//
// requirements:
// Windows Scripting Host 1.0 or better. This is included with Windows
// 98/Me/2000/XP. Users of Windows 95 or Windows NT 4.0 need to download
// and install WSH support from
// http://msdn.microsoft.com/scripting/.
//
// usage:
// <exec executable="cscript.exe">
// <env key="ANTRUN_TITLE" value="Title for Window" /> <!-- optional -->
// <env key="ANTRUN_OUTPUT" value="output.log" /> <!-- optional -->
// <arg value="//NoLogo" />
// <arg value="antRunAsync.js" /> <!-- this script -->
// <arg value="real executable" />
// </exec>
var WshShell = WScript.CreateObject("WScript.Shell");
var exeStr = "%comspec% /c";
var arg = "";
var windowStyle = 1;
var WshProcessEnv = WshShell.Environment("PROCESS");
var windowTitle = WshProcessEnv("ANTRUN_TITLE");
var outputFile = WshProcessEnv("ANTRUN_OUTPUT");
var OS = WshProcessEnv("OS");
var isWindowsNT = (OS == "Windows_NT");
// On Windows NT/2000/XP, specify a title for the window. If the environment
// variable ANTRUN_TITLE is specified, that will be used instead of a default.
if (isWindowsNT) {
if (windowTitle == "")
windowTitle = "Ant - " + WScript.Arguments(i);
exeStr += "title " + windowTitle + " &&";
}
// Loop through arguments quoting ones with spaces
for (var i = 0; i < WScript.Arguments.count(); i++) {
arg = WScript.Arguments(i);
if (arg.indexOf(' ') > 0)
exeStr += " \"" + arg + "\"";
else
exeStr += " " + arg;
}
// If the environment variable ANTRUN_OUTPUT was specified, redirect
// output to that file.
if (outputFile != "") {
windowStyle = 7; // new window is minimized
exeStr += " > \"" + outputFile + "\"";
if (isWindowsNT)
exeStr += " 2>&1";
}
// WScript.Echo(exeStr);
// WshShell.Run(exeStr);
WshShell.Run(exeStr, windowStyle, false);
<exec executable="cscript.exe">
<env key="ANTRUN_TITLE" value="Title for Window" /> <!-- optional -->
<env key="ANTRUN_OUTPUT" value="output.log" /> <!-- optional -->
<arg value="//NoLogo" />
<arg value="antRunAsync.js" /> <!-- this script -->
<arg value="real executable" />
</exec>
Another workaround for Windows is to schedule a permanent task and force running it from the Ant script.
For example, run the command:
C:\>SCHTASKS /Create /RU SYSTEM /SC ONSTART /TN Tomcat /TR
"C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin\startup.bat"
Note, that
ONSTART
can be replaced with
ONCE
if you do not want to keep Tomcat running.
Add the following code to your Ant script:
<exec executable="SCHTASKS">
<arg value="/Run"/>
<arg value="/TN"/>
<arg value="Tomcat"/>
</exec>