控制 Bash 守护进程启动的进程

发布于 2024-08-19 08:37:44 字数 1110 浏览 6 评论 0原文

在 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

In bash, I have created a simple daemon to execute commands when my internet connection changes:

#!/bin/bash

doService(){
    while
    do  
    checkTheInternetConnection
    sleep 15
    done
}

checkTheInternetConnection(){
    if unchanged since last check
        return
    else
        execute someCommand
    fi
}

someCommand(){
    do something
}

doService

And this has been working pretty well for what I need it to do.

The only problem is that as a part of my "someCommand" and "checkTheInternetConnection" I use other built-in utilities like arp, awk, grep, head, etc.

However, 99% of the time, I will just need arp.

First question: Is it necessary to keep the other commands open? Is there a way to kill a command once I've already processed its output?


Another question: (MOVED TO AN NEW POST)
I am having a hell of a time trying to write a "kill all other daemon processes" function. I do not ever want more than one daemon running at once. Any suggestions? This is what I have:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

折戟 2024-08-26 08:37:44

您能详细说明第一个问题吗?我认为您是在询问如何通过管道运行多个命令(cat xxx|grep yyy|tail -zzz)。

每个命令将继续运行,直到其管道有数据(未达到 EOF)。因此,在此示例中,grep 仅在 cat 处理完所有输入并关闭其管道末端后才会退出。但这里有一个技巧,如果 grep 已经读取了所有(至少是缓冲的)输入,那么 cat 只会关闭管道的末端,因为管道中的写入调用是阻塞的。因此,您在设计脚本时需要牢记这一点。

但我认为您不应该担心内置实用程序。一般来说,如果这是一个问题的话,它们的内存占用量较低。

Can you detail more the first question? I think you are asking about running many commands piped together (cat xxx|grep yyy|tail -zzz).

Each command will keep running until its pipe has data (not reached EOF). So in this example grep will only exit after cat processed all the input and closed its end of the pipe. But there is a trick here, cat will only close its end of the pipe if grep already read all (buffered, at least) the input, because the writing call in pipes are blocking. So you need to have this in mind while designing your scripts.

But I don't think you should worry about the built-in utilities. Generally they have a low memory footprint, if that is the concern.

于我来说 2024-08-26 08:37:44

对于你的第一个问题。我不太明白,但我可以看出您可能在问两件事之一。

  1. 您在 bash 函数(grep、awk、sed 等)中运行内容,并且由于该函数长时间运行,您担心您运行的实用程序以某种方式保持打开状态。
  2. 您正在将输出从一个命令传输到另一个命令,并且担心该命令在完成运行后仍保持打开状态。

1 和 2 都不会在实用程序命令完成运行后让它们保持“打开”状态。您可以通过放入

ps -ef | grep "command" | grep -v 'grep'

整个代码来查看以该名称运行的内容来证明这一点。或者

ps -ef | grep "$" | grep -v 'grep'

它将列出当前进程已生成的内容。

更新:

所以,您似乎对事物如何从管道运行感兴趣。您可以使用以下命令直观地看到这一点:

$ ls / | grep bin | grep bin | ps -ef | grep ls
$

将其与类似的命令进行比较:

$ find ~ | grep bin | ps -ef | grep find
$

请注意“ls”不再在进程列表中,但 find 却在进程列表中。您可能需要在管道中添加更多“grep bin”命令才能获得效果。一旦第一个命令完成输出,即使其余命令尚未完成,它也会关闭。其他命令将在处理完第一个命令的输出后完成(因此是管道性质)

For your first question. I don't quite understand it fully, but I can see that you may be asking one of two things.

  1. You run things in a bash function (grep, awk, sed, etc) and because that function is long running, you are afraid that the utilities that you run are somehow remaining open.
  2. You are piping output from one command to another and are afraid that the command stays open after it has finished running.

Neither 1 nor 2 will leave utility commands "open" after they are finished running. You could prove this by putting in

ps -ef | grep "command" | grep -v 'grep'

throughout the code to see just what is running by that name. or

ps -ef | grep "$" | grep -v 'grep'

which will list out things that the current process has spawned.

UPDATE:

So, it seems that you are interested with how things run from a pipe. You can see this visually using the following command:

$ ls / | grep bin | grep bin | ps -ef | grep ls
$

compare that with something like:

$ find ~ | grep bin | ps -ef | grep find
$

Notice how the 'ls' is no longer in the process list, but the find is. You may have to add more "grep bin" commands into the pipeline to get the effect. Once the first command is finished outputting, it will close, even if the rest of the commands are not yet finished. The other commands will finish as they are done processing the output from the first (thus the pipe nature)

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