0

I want to make job on Jenkins that starts server (MockServer on WireMock).

Server is launched from *.jar file, from terminal like that.

java -jar serverLaunch.jar

It takes over my console. To avoid that I modify this and do:

java -jar serverLaunch.jar &>/dev/null &

And that works for me on my local PC. Now I want to move it to Jenkins.

If I try to do this from "Shell command" block in Jenkins Job then:

a) java -jar serverLaunch.jar

  • I have task locked in queue in my Jenkins and I don't want that but server starts and works.

b) java -jar serverLaunch.jar &>/dev/null &

  • Job ends with success but my server is not alive.

I have wrapped this command also in .sh script and .rb script. Any idea how to make it work?


I've tried this: https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build

And then in Jenkins "Shell script":

daemonize -E BUILD_ID=dontKillMe /bin/bash launch.sh

But it also passes but server is not alive.

F1sher
  • 7,140
  • 11
  • 48
  • 85

3 Answers3

6

I had to check "Inject environment variables to the build process" and add:

BUILD_ID=dontKillMe

Now it is working.

F1sher
  • 7,140
  • 11
  • 48
  • 85
  • Setting BUILD_ID=dontKillMe or JENKINS_NODE_COOKIE=dontKillMe doesn't work for me. Any suggestions on how to run jetty server or any shell command in background as daemon. – pavanlapr Jun 21 '18 at 00:42
-1

Try using nohup e.g.: nohup java -jar serverLaunch.jar &

That should prevent the process being terminated when the parent shell process exits (which I suspect is your problem).

Tom
  • 3,471
  • 21
  • 14
  • When i do: `nohup java -jar serverLaunch.jar &` then task ends with success but I can't connect to server cuz its offline – F1sher Nov 23 '16 at 11:49
  • nohup leaves a log file called nohup.out in the directory you started it from. Might be worth having a look at that to see if there's an error being reported. – Tom Nov 23 '16 at 18:04
  • 1
    no, it does not work. Jenkins kills the process that was started in job, even if it is daemonized with nohup (or 'daemonize' as the OP did) when the BUILD_ID env var is not set in job. – Andrey Regentov Feb 21 '17 at 11:24
-1

Another effective approach would be to add a post-build action that executes a shell spawning the server.

ang3lkar
  • 109
  • 1
  • 8