从同一脚本杀死其他 bash 守护进程

发布于 2024-08-19 13:06:08 字数 765 浏览 4 评论 0原文

我在尝试编写一个“杀死所有其他守护进程”函数以在 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 技术交流群。

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

发布评论

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

评论(3

演出会有结束 2024-08-26 13:06:08

您已经使用 grep -v 消除了当前进程 ID,因此当您发出 kill 时没有理由再次执行此操作。也没有理由在变量中构建 kill 。只需执行:

kill $otherprocess

但为什么不直接使用:

pkill -v $ BashScriptName

pkill -v $ $0

不使用任何 grep。

然后你可以这样做:

if [[ $? ]]
then
    WriteLogLine "Other daemons killed."
else
    WriteLogLine "There are no daemons running."
fi

You've already eliminated the current process ID using grep -v so there's no reason to do it again when you issue the kill. There's also no reason to build the kill in a variable. Just do:

kill $otherprocess

But why not just use:

pkill -v $ BashScriptName

or

pkill -v $ $0

without any grep.

Then you can do:

if [[ $? ]]
then
    WriteLogLine "Other daemons killed."
else
    WriteLogLine "There are no daemons running."
fi
枉心 2024-08-26 13:06:08

你能在这里尝试一下旧的“锁定文件”技巧吗?测试文件:如果不存在则创建并启动;否则退出。

例如:

#!/bin/bash
LOCKFILE=/TMP/lockfile
if [ -f "$LOCKFILE" ]; then
        echo "Lockfile detected, exiting..."
        exit 1
fi
touch $LOCKFILE
while : 
do
        sleep 30
done
rm $LOCKFILE # assuming an exit point here, probably want a 'trap'-based thing here.

缺点是如果留下了孤儿,您必须不时清理锁定文件。

您能否将其转换为“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:

#!/bin/bash
LOCKFILE=/TMP/lockfile
if [ -f "$LOCKFILE" ]; then
        echo "Lockfile detected, exiting..."
        exit 1
fi
touch $LOCKFILE
while : 
do
        sleep 30
done
rm $LOCKFILE # assuming an exit point here, probably want a 'trap'-based thing here.

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

淡看悲欢离合 2024-08-26 13:06:08

如果您运行您的服务 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.

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