从同一脚本杀死其他 bash 守护进程
我在尝试编写一个“杀死所有其他守护进程”函数以在 bash 守护进程中使用时遇到了麻烦。我不想同时运行多个守护进程。有什么建议吗?这就是我所拥有的:
#!/bin/bash
doService(){
while
do
something
sleep 15
done
}
killOthers(){
otherprocess=`ps ux | awk '/BashScriptName/ && !/awk/ {print $2}'| grep -Ev $$`
WriteLogLine "Checking for running daemons."
if [ "$otherprocess" != "" ]; then
WriteLogLine "There are other daemons running, killing all others."
VAR=`echo "$otherprocess" |grep -Ev $$| sed 's/^/kill /'`
`$VAR`
else
WriteLogLine "There are no daemons running."
fi
}
killOthers
doService
它有时有效,有时则无效。几乎没有什么是一致的。
I am having a hell of a time trying to write a "kill all other daemon processes" function for use within a bash daemon. I do not ever want more than one daemon running at once. Any suggestions? This is what I have:
#!/bin/bash
doService(){
while
do
something
sleep 15
done
}
killOthers(){
otherprocess=`ps ux | awk '/BashScriptName/ && !/awk/ {print $2}'| grep -Ev $`
WriteLogLine "Checking for running daemons."
if [ "$otherprocess" != "" ]; then
WriteLogLine "There are other daemons running, killing all others."
VAR=`echo "$otherprocess" |grep -Ev $| sed 's/^/kill /'`
`$VAR`
else
WriteLogLine "There are no daemons running."
fi
}
killOthers
doService
It works some of the time, it doesn't others. There is almost nothing consistent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经使用
grep -v
消除了当前进程 ID,因此当您发出kill
时没有理由再次执行此操作。也没有理由在变量中构建kill
。只需执行:但为什么不直接使用:
或
不使用任何 grep。
然后你可以这样做:
You've already eliminated the current process ID using
grep -v
so there's no reason to do it again when you issue thekill
. There's also no reason to build thekill
in a variable. Just do:But why not just use:
or
without any grep.
Then you can do:
你能在这里尝试一下旧的“锁定文件”技巧吗?测试文件:如果不存在则创建并启动;否则退出。
例如:
缺点是如果留下了孤儿,您必须不时清理锁定文件。
您能否将其转换为“rc”(或 S*/K* 脚本?),以便您可以在 inittab 中指定“once”(或等效方法 - 在 MacOS 上不确定)?
就像这里描述的那样:
http://aplawrence.com/Unixart/startup.html
编辑:
可能这个Apple Doc可能会有所帮助:
http ://developer.apple.com/mac/library/DOCUMENTATION/MacOSX/Conceptual/BPSystemStartup/Articles/StartupItems.html
Could you try the old 'lock file' trick here? Test for a file: if it doesn't exists, create it and then startup; otherwise exit.
Like:
The downside is you have to clean-up lock-files from time to time, if an orphan is left behind.
Can you convert this to a 'rc' (or S*/K* script ?) so you can specify 'once' in the inittab (or equivalent method - not sure on MacOS) ?
Like what is described here:
http://aplawrence.com/Unixart/startup.html
EDIT:
Possibly this Apple Doc might help here:
http://developer.apple.com/mac/library/DOCUMENTATION/MacOSX/Conceptual/BPSystemStartup/Articles/StartupItems.html
如果您运行您的服务 下 runit - 服务不得分叉到后台 - 你'我们将保证只有一个实例正在运行。如果服务没有运行或者退出或崩溃,runit 会启动该服务,如果您要求则停止它,并保留 pid 文件。
If you run your service under runit — the service mustn't fork into the background — you'll have a guarantee there is exactly one instance of it running. runit starts the service if it isn't running or if it quit or crashed, stops it if you ask, keeps a pidfile around.