linux - 在 bash 中将正在运行的函数发送到后台

标签 linux bash shell

正如标题所说,我想将一个正在运行的函数发送到后台。有点像

function myfunc {
    some_command | while read line
    #some_command must be running indefinitely
    do
        #if some condition on ${line} satisfies
        #Do something and go to background
    done
}

这可能吗?

我知道可以直接调用一个函数并直接在后台运行它。这是很明显的。这不是 this 的副本.

最佳答案

一个简单的命令在后台创建一个进程并等待其终止,在您的情况下,调用者必须在与继续运行的被调用者异步满足条件时继续。

这可以通过 kill -STOP(调用者阻止自己)和 kill -CONT(被调用者取消阻止调用者)来完成

function myfunc {
    some_command | while read line
    #some_command must be running indefinitely
    do
        #if some condition on ${line} satisfies
        kill -CONT $$
    done & kill -STOP $$
}

关于linux - 在 bash 中将正在运行的函数发送到后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45284258/

相关文章:

linux - 如何将多行linux命令转换为一行命令

python - 我将如何使用 php 在网页上显示多个按钮(它们将控制 raspberry pi)

linux - 使用 Linux 剪切、排序和 uniq

linux - 为什么在存在目标的情况下,bash 中的否定文件存在检查会返回奇怪的结果?

linux - 运行命令而不让我等待

linux - 读取时间戳文件并创建目录结构取决于时间戳

linux - 无法启动 mongodb 服务

python - 在 bash 脚本中返回 Python 脚本的退出号和值

java - 使用Bash ssh在多台远程机器上运行Java程序

bash - 易于并行化