How to fork a background process in Jenkins? Setti

2019-02-15 00:42发布

问题:

I have a maven job in Jenkins(version 1.578). The maven snippet (Ant script written in Groovy code) below starts a Fuse server, and I cannot make Jenkins to leave it running after the job finishes.

<profile>
    <id>start-fuse</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-fuse</id>
                        <phase>integration-test</phase>
                        <goals>
                          <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                            <![CDATA[
                                def fuseBinDirectory = '${fuse.dir}/bin'
                                ant.exec(executable:'bash', dir:fuseBinDirectory, failonerror:true, osfamily:'unix') {
                                    ant.env(key:'BUILD_ID',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
                                    ant.env(key:'KARAF_OPTS',value:'${KARAF_OPTS}')
                                    ant.arg(value:"-c")
                                    ant.arg(value:"nohup ./start ${fuse.start.parameters} </dev/null >> nohup.out 2>&1&")
                                }
                            ]]>
                            </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

What do I wrong?

How can I fork a similar Fuse process under Windows so that Jenkins does not kill it?

UPDATE - SOLUTION

Adding the 4 env entries mentioned in the accepted answer works both on windows and linux. So no matter which job type you have you can have something like this:

ant.exec(executable:'sh', dir:fuseBinDirectory, failonerror:true, osfamily:'unix') {
    ant.env(key:'BUILD_ID',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
    ant.env(key:'HUDSON_SERVER_COOKIE',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
    ant.env(key:'JENKINS_COOKIE',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
    ant.env(key:'JENKINS_SERVER_COOKIE',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
    ant.env(key:'HUDSON_COOKIE',value:'DO_NOT_STOP_MY_PROCESS_JENKINS')
    ant.env(key:'KARAF_OPTS',value:'${KARAF_OPTS}')
    ant.arg(line:'-c "./start ${fuse.start.parameters}"')
}

Nohup seems to be non neccessary at all.

回答1:

If you will notice the comment by user 'mdp' in this page, you will notice that in case of jobs created using freestyle template, BUILD_ID does work. There are many others who faced similar issue. Such issues were seen only when it was a non-freestyle job.

Alternatively, should also check unsetting the following variables: HUDSON_COOKIE, HUDSON_SERVER_COOKIE, JENKINS_COOKIE or JENKINS_SERVER_COOKIE as suggested by few (Comments section) in this link.