Bash 脚本杀死并重新启动 Hudson

发布于 2024-09-14 10:39:52 字数 526 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-09-21 10:39:52

这代表了对脚本的一些直接改进:

#!/bin/bash
h=$(pgrep -f hudson)    # $() is preferred over backticks
if [[ -n $h ]]; then    # this checks whether a variable is non-empty
  kill $h
  while [[ -n $h ]]; do
    sleep 1
    h=$(pgrep -f hudson)    # it's usually unnecessary to unset a variable before you set it
  done
fi
java -jar ~/hudson/hudson.war &

但是,这可能就是您所需要的(或使用提供的工具 mrooney 提到):

while pkill hudson; do sleep 1; done
java -jar ~/hudson/hudson.war &

This represents some straightforward improvements to your script:

#!/bin/bash
h=$(pgrep -f hudson)    # $() is preferred over backticks
if [[ -n $h ]]; then    # this checks whether a variable is non-empty
  kill $h
  while [[ -n $h ]]; do
    sleep 1
    h=$(pgrep -f hudson)    # it's usually unnecessary to unset a variable before you set it
  done
fi
java -jar ~/hudson/hudson.war &

However, it's likely that this is all you need (or use the provided facility that mrooney referred to):

while pkill hudson; do sleep 1; done
java -jar ~/hudson/hudson.war &
走走停停 2024-09-21 10:39:52

对哈德逊好一点,让它自行关闭怎么样?我在 Hudson 论坛中发现了以下声明:

我添加了 http://server/hudson/exit
1.161.访问此 URL 将关闭运行 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:

I added http://server/hudson/exit to
1.161. Accessing this URL will shutdown the VM that runs Hudson.

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.

泪冰清 2024-09-21 10:39:52

如果您通过 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文