【发布时间】:2010-01-27 17:59:04
【问题描述】:
在 bash 中,我创建了一个简单的守护进程来在我的互联网连接发生变化时执行命令:
#!/bin/bash
doService(){
while
do
checkTheInternetConnection
sleep 15
done
}
checkTheInternetConnection(){
if unchanged since last check
return
else
execute someCommand
fi
}
someCommand(){
do something
}
doService
这对于我需要它做的事情来说效果很好。
唯一的问题是,作为“someCommand”和“checkTheInternetConnection”的一部分,我使用了其他内置实用程序,如 arp、awk、grep、head 等。
但是,在 99% 的情况下,我只需要 arp。
第一个问题:是否有必要保持其他命令打开?一旦我已经处理了它的输出,有没有办法杀死一个命令?
另一个问题:(移至新帖子) 我正在尝试编写一个“杀死所有其他守护进程”功能的地狱。我不希望同时运行一个以上的守护进程。有什么建议么?这就是我所拥有的:
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
【问题讨论】:
-
最好将第二个问题拆分成一个新帖子。