Bash 脚本杀死并重新启动 Hudson
我是 Bash 脚本新手,但学得很快。通常。我正在尝试编写一个脚本来终止并重新启动 Hudson 实例 - 需要重新启动它才能获取环境变量中的更改。到目前为止我所得到的:
#!/bin/bash
h=`pgrep -f hudson`
if test "$h" != ""; then
kill $h
while [ "$h" != "" ]; do
sleep 1
unset h
h=`pgrep -f hudson`
done
fi
java -jar ~/hudson/hudson.war &
该脚本正确地确定了正在运行的 Hudson 实例的 PID 并杀死它。然而,它只是在“kill”行之后等待,并且不会继续。如果我按下某个键,它就会完成终止进程并退出脚本,甚至不会进入 while 循环。显然我遗漏了一些关于如何终止进程的信息。并不是Hudson进程挂起并且没有响应“kill”;它会正常退出,直到我干预为止。
我也确信这会更有效,但现在我只想了解我哪里出了问题。
提前致谢。
I am a novice at Bash scripting but I'm a quick learner. Usually. I'm trying to write a script to kill and restart an instance of Hudson--it needs to be restarted to pick up changes in environment variables. What I have so far:
#!/bin/bash
h=`pgrep -f hudson`
if test "$h" != ""; then
kill $h
while [ "$h" != "" ]; do
sleep 1
unset h
h=`pgrep -f hudson`
done
fi
java -jar ~/hudson/hudson.war &
The script correctly determines the running Hudson instance's PID and kills it. However, it just waits after the "kill" line and doesn't proceed. If I hit a key there it completes killing the process and exits the script, never even getting to the while loop. Clearly I'm missing something about how the process should be killed. It's not that the Hudson process is hung and not responding to "kill"; it exits normally, just not until I intervene.
I'm also sure this could be much more efficient but right now I would just like to understand where I'm going wrong.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这代表了对脚本的一些直接改进:
但是,这可能就是您所需要的(或使用提供的工具 mrooney 提到):
This represents some straightforward improvements to your script:
However, it's likely that this is all you need (or use the provided facility that mrooney referred to):
对哈德逊好一点,让它自行关闭怎么样?我在 Hudson 论坛中发现了以下声明:
您可以使用 wget 调用该 URL。如果哈德逊没有在适当的时间关闭,你仍然可以杀死它。
编辑:我刚刚偶然发现 另一个线程,带有有趣的重启选项。它使用 Winstone 服务器中构建的命令。不确定它是否会接受环境变量的更改。
How about being nice to Hudson and let it shut down itself. I found the following statement in the Hudson forum:
You can call the URL with wget. You can still kill Hudson if it doesn't shut down in an appropriate time.
EDIT: I just stumbled over another thread, with interesting restart options. It uses commands of the build in Winstone server. Not sure if it will pick up changes to environment variables.
如果您通过 RPM 使用 Hudson,它已经附带了一个 init 脚本。如果没有,我会看一下它们,看看您是否可以以它们为基础编写脚本: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main/rpm/SOURCES/(访客//访客)。
If you are using Hudson via an RPM, it comes with an init script already. If not, I'd take a look at them and see if you can base your script off of them: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main/rpm/SOURCES/ (guest//guest).