bash 从 Jenkins 在后台启动 shell 脚本的简洁方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37160402/
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
Clean way of launching a shell script in background from Jenkins
提问by garci560
What's the proper way to launch a script from jenkins, don't get the build hanging, and leave the process running? I can't seem to get it to work. Either the script doesn't run or the build hangs.
从 jenkins 启动脚本的正确方法是什么,不要挂起构建,并使进程继续运行?我似乎无法让它工作。脚本不运行或构建挂起。
If I put in the build's "Execute shell" step bash relaunch.sh &
or relaunch.sh > output.log &
or nohup bash relaunch.sh &
,
nothing happens; build finishes, but the process doesn't run. I guess it can be related to Jenkins waiting for the error pipe to close.
如果我把生成的“执行shell”的步骤bash relaunch.sh &
或relaunch.sh > output.log &
或者nohup bash relaunch.sh &
,什么都不会发生; 构建完成,但进程没有运行。我想这可能与 Jenkins 等待错误管道关闭有关。
If I do
nohup bash relaunch.sh 2>&1 > output.log
as suggested here, the output is properly redirected, but the build hangs (doesn't finish), and the process dies when I kill the build.
如果我
nohup bash relaunch.sh 2>&1 > output.log
按照此处的建议进行操作,则输出会正确重定向,但构建会挂起(未完成),并且当我终止构建时该进程会终止。
Adding export BUILD_ID=dontKillMe
, as suggested here, here, and here, either to the "Execute shell" step or the script itself doesn't help either. The build hangs and the process dies when I kill the build. Needless to say, my knowledge of linux is very limited.
export BUILD_ID=dontKillMe
按照此处、此处和此处的建议添加到“执行 shell”步骤或脚本本身也无济于事。当我终止构建时,构建挂起并且进程终止。不用说,我对 linux 的了解非常有限。
How do people do this in a clean way?
人们如何以一种干净的方式做到这一点?
回答by Inian
A convenient way to achieve that is to change the environment variable BUILD_ID
under Execute shellwhich Jenkins's
ProcessTreeKilleris looking for.
一个方便的方式实现这一目标是改变环境变量BUILD_ID
下执行shell其Jenkins's
ProcessTreeKiller所期待的。
By doing,
通过做,
BUILD_ID=dontKillMe nohup bash relaunch.sh &
Jenkins will assume that the background job is not spawned by the build and will not kill them after finishing the job.
Jenkins 将假定后台作业不是由构建生成的,并且在完成作业后不会杀死它们。
Thanks to Joshuafor his observation that you could also use JENKINS_NODE_COOKIE
as
感谢Joshua的观察,您也可以将其JENKINS_NODE_COOKIE
用作
JENKINS_NODE_COOKIE=dontKillMe
回答by user2671131
I was having the exact same problem. I ended up fixing this by placing the following in the Jenkins execute shell box:
我遇到了完全相同的问题。我最终通过在 Jenkins 执行 shell 框中放置以下内容来解决此问题:
BUILD_ID=dontKillMe ./grid.sh
I moved the &
inside the script file. Here's what the script looks like:
我移动了&
脚本文件的内部。脚本如下所示:
#!/bin/bash
java -jar selenium-server-standalone-3.0.1.jar -role hub &
Hopefully this helps someone!
希望这对某人有所帮助!
回答by GrueMaster
Interesting solutions. Has anyone tried screen?
有趣的解决方案。有人试过屏幕吗?
Here's how I run it (for RTL simulation using ModelSim-ASE):
这是我的运行方式(使用 ModelSim-ASE 进行 RTL 仿真):
screen -S $testname -d -m -L bash -c 'cd build_sim ; make; make sim'
echo Waiting for simulator ...
I then wait for our simulator to get to a waiting state by watching the screenlog.0
然后我通过观看 screenlog.0 等待我们的模拟器进入等待状态
until ((`fgrep -c 'Ready for simulation' screenlog.0`)) ;do
sleep 1
done
Then, when the other jobs that need the simulation are done running (or Jenkins detects a failure), in the post, run:
然后,当其他需要模拟的作业运行完毕(或 Jenkins 检测到故障)时,在帖子中运行:
screen -S $testname -X kill
The only caveat is that if Jenkins somehow dies before the job cleans up, in theory you could have a simulator chewing up resources while waiting for something to happen.
唯一的警告是,如果 Jenkins 在工作清理之前以某种方式死了,理论上你可以让模拟器在等待某事发生的同时咀嚼资源。
To break it down a little: With screen, '-S' assigns a title to the session (or addresses a running session with that title), '-d' runs detached, '-m' spawn a fork first (which hides from Jenkins watchful eyes), and -L turns on logging (which is also handy in case something in the simulator breaks - you now have a log file artifact with the output). The rest is simply bash inside of the screen session.
稍微分解一下:使用 screen,'-S' 为会话分配一个标题(或使用该标题解决正在运行的会话),'-d' 运行分离,'-m' 首先生成一个叉子(隐藏Jenkins 警惕的眼睛),并且 -L 打开日志记录(这在模拟器中的某些内容中断时也很方便 - 您现在有一个带有输出的日志文件工件)。其余的只是在屏幕会话中 bash 。