java 无法从 jenkins 运行 nohup 命令作为后台进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37341545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:24:46  来源:igfitidea点击:

Unable to run nohup command from jenkins as a background process

javalinuxunixjenkinsnohup

提问by Utsav Gupta

UPDATE: Based on below discussion I have edited my answer for more accurate description.

更新:基于以下讨论,我编辑了我的答案以获得更准确的描述。

I am trying to run a nohup command from jenkins. The full command is

我正在尝试从 jenkins 运行 nohup 命令。完整的命令是

nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &

This command does not work. I can see status as success in jenkins but no java process in linux. When I do 'ps -ef | grep java'

此命令不起作用。我可以在 jenkins 中看到状态为成功,但在 linux 中没有 java 进程。当我做 'ps -ef | grep java'

However when I remove the last '&' , that is I change it from run in forground instead of background

但是,当我删除最后一个 '&' 时,我将其从在前台运行而不是在后台运行更改

It starts working. I can see the java process started.

它开始工作。我可以看到java进程启动了。

The original command works fine If I run it on linux console.

如果我在 linux 控制台上运行它,原始命令工作正常。

I need to run it from jenkins in the original form that is as a backgorund process. So that it is independant of jenkins.

我需要以原始形式从 jenkins 运行它,作为 backgorund 过程。这样它就独立于詹金斯。

Any clues why is this happening?

任何线索为什么会发生这种情况?

回答by Joao Vitor Marques

In your jenkins shell script try:

在您的 jenkins shell 脚本中尝试:

  export BUILD_ID=dontKillMe
  nohup java -jar your_java_app.jar &

It worked for me!

它对我有用!

回答by jpyams

Long story short, Jenkins kills all processes spawned by a job once that job finishes. To override this behavior, you need to set an environment variable.

长话短说,一旦该作业完成,Jenkins 就会终止该作业产生的所有进程。要覆盖此行为,您需要设置一个环境变量。

The variable appears to vary from job type to job type. It used to be BUILD_ID, but for Pipeline jobs it is JENKINS_NODE_COOKIE, and there are several others mentioned in this answer.

该变量似乎因工作类型而异。它曾经是BUILD_ID,但对于流水线作业来说是这样JENKINS_NODE_COOKIE,并且在这个答案中还提到了其他几个。

So if you're running your command in Pipeline, it would look like this:

所以如果你在 Pipeline 中运行你的命令,它看起来像这样:

sh 'JENKINS_NODE_COOKIE=dontKillMe nohup java -jar /home/.../jar/server-process-0.35.jar prod >> /var/../server-process-prod.log 2>&1 &'

See the wiki on ProcessTreeKillerand this comment in the Jenkins Jirafor more information.

有关更多信息,请参阅ProcessTreeKiller 上的 wikiJenkins Jira 中的此评论

回答by PyTux

I tried every possible combination with BUILD_ID but it didn't work. I made it though by putting "nohup command > output.txt&" inside a shell script ran by the execute shell in jenkins, it worked perfectly!

我尝试了 BUILD_ID 的所有可能组合,但没有奏效。我通过将“nohup command > output.txt&”放在由 jenkins 中的执行 shell 运行的 shell 脚本中来实现,它工作得很好!

回答by Walid

Best simple solution is to use "at now" instead of "nohup"

最好的简单解决方案是使用“现在”而不是“nohup”

In your job jenkins (execute shell) put :

在你的工作 jenkins (execute shell) 中输入:

set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min

回答by Michu93

Got the same problem, added:

遇到同样的问题,补充:

BUILD_ID=dontKillMe python /var/lib/jenkins/release.py

into Execute Shell -> Command and inside release.py there is:

进入 Execute Shell -> Command 并在 release.py 中有:

os.system('nohup java -jar ' + new_jars_on_server + '/' + generated_jar_by_mvn_name + '&')

and it works

它有效

回答by millevlada

what worked for me was wrapping the nohup java -jar ...command into sh file inside execute shell command, and running that same sh file right after:

对我有用的是将nohup java -jar ...命令包装到执行 shell 命令内的 sh 文件中,然后立即运行相同的 sh 文件:

echo "starting java jar..."
cd [some location where jar is]
echo "nohup java -jar [jar_name].jar &" > start-jar-in-background.sh
sh start-jar-in-background.sh
echo "started java jar"

If I had nohup java -jar ...inline with Execute shell command, then it didn't start it from some reasons. I spent quite some time on this, hope it helps to someone ';)

如果我有nohup java -jar ...与 Execute shell 命令内联,那么它由于某些原因没有启动它。我花了很多时间在这上面,希望它对某人有所帮助';)

回答by Tom CLERC

Simplest way :

最简单的方法:

`nohup java -jar [jar_name].jar >log_file_you_want 2>another_file`&

`nohup java -jar [jar_name].jar >log_file_you_want 2>another_file`&