从自定义 init.d 脚本输出标准 OK / FAILED 消息
我对我正在编写的初始化脚本有一些疑问。我之前实际上并没有从头开始做过,所以可能有一些我不知道的提示和技巧。另外,shell 脚本并不是我经常使用的东西,因此语法需要一些时间来适应。
首先,我有一个函数来查看应用程序是否正在运行。这样做的原因,而不是 lockfiles 或类似的,是我只想要一个实例,并且应用程序有点错误,即使主应用程序被终止,插件有时也会徘徊。
所以我有类似的东西。
isrunning() {
pids=`ps aux | grep -e 'FooBar' | grep -v grep | awk '{print $2}'`
if [ ! -n "$pids" ]; then
return 1
else
return 0
fi
}
有更好的办法吗?
我启动主应用程序,
daemon --user apache "./FooBar"
但将脚本执行挂在该行上,直到 FooBar 终止。
daemon --user apache "FooBar&"
虽然有效。但看起来运行init脚本时出现的[OK] / [FAILED]实际上是daemon函数输出的?因此,即使执行失败,daemon 仍然会报告 [OK],因为 FooBar 的进程已分离。
有没有办法让守护进程在不分离进程的情况下不被卡住?我尝试过 -d 选项等,但没有运气。
否则我想只做一个sleep 3,然后检查isrunning,效果很好。但是我不知道如何以初始化脚本通常所做的漂亮方式打印 [OK] / [FAILED] 消息。是否有一个函数可以用来以标准形式打印它?
更新
我找到了打印消息的解决方案。看起来它实际上是用于此的功能。 成功和失败将打印消息。
但守护进程问题仍然存在。有什么办法可以摆脱 & 吗?
更新2
我找到了一种解决方法,并使用
su -m -c "./FooBar&" apache
并检查应用程序之后是否正在运行。根据结果我打印正确的消息。
I have a few questions regarding an init script I'm writing. I haven't actually done it from scratch before so there are probably several tips and tricks I don't know about. Also, shell script is not something I have used a lot so the syntax took a bit of getting used to.
First of all I have a function to see if the application is running. The reason for this, instead of lockfiles or similar, is that I only want ONE instance and the application is a bit buggy and plugins can sometime linger even if the main application is killed.
So I have something like.
isrunning() {
pids=`ps aux | grep -e 'FooBar' | grep -v grep | awk '{print $2}'`
if [ ! -n "$pids" ]; then
return 1
else
return 0
fi
}
Is there a better way?
I start the main application with
daemon --user apache "./FooBar"
but that hung the script execution on that line until FooBar was terminated.
daemon --user apache "FooBar&"
works though. But it looks like the [OK] / [FAILED] that shows up when running the init script is actually output by the daemon function? So even if the execution fails, daemon still reports [OK] since the process of FooBar is detached.
Is there a way to get daemon NOT to get stuck without detaching the process? I have tried the -d option etc but no luck.
Otherwise I was thinking of just doing a sleep 3 and then check isrunning which works fine. But then I don't know how to print the [OK] / [FAILED] messages in the nice pretty way that init scripts normally do. Is there perhaps a function one can use to print this in the standard form?
UPDATE
I found the solution to printing the messages. It seems it actually are functions for this.
success and failure will print the messages.
The daemon issue remain though. Any way to get rid of &?
UPDATE 2
I found a workaround and used
su -m -c "./FooBar&" apache
and checking if the application was running after that. Depending on the result I print the correct message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的脚本是常见问题解答的错误答案。尝试这个更简单的版本:
我也非常想从 awk 中设置退出代码:
如果您使用的是 Linux,也许您应该使用 pidof 来代替。
The script you are using is the wrong answer to a FAQ. Try this simpler version instead:
I'm very very tempted to set the exit code from within awk as well:
If you are on Linux, perhaps you should be using
pidof
instead, though.我找到了打印消息的解决方案。看起来它实际上是用于此的功能。成功和失败将打印消息。
I found the solution to printing the messages. It seems it actually are functions for this. success and failure will print the messages.