相关疑难解决方法(0)

如何以优雅的方式永远什么都不做?

我有一个程序可以生成有用的信息,stdout但也可以从stdin. 我想将其标准输出重定向到一个文件,而不在标准输入上提供任何内容。到目前为止,很好:我可以做到:

program > output
Run Code Online (Sandbox Code Playgroud)

并且不要在 tty 中做任何事情。

但是,问题是我想在后台执行此操作。如果我做:

program > output &
Run Code Online (Sandbox Code Playgroud)

程序将被挂起(“挂起(tty 输入)”)。

如果我做:

program < /dev/null > output &
Run Code Online (Sandbox Code Playgroud)

程序立即终止,因为它到达 EOF。

似乎我需要的是将program某些内容导入到无限期不执行任何操作且不读取的内容中stdin。以下方法有效:

while true; do sleep 100; done | program > output &
mkfifo fifo && cat fifo | program > output &
tail -f /dev/null | program > output &
Run Code Online (Sandbox Code Playgroud)

然而,这一切都非常难看。有是一种优雅的方式,使用标准的Unix工具,以“什么都不做,无限期”(意译man true)。我怎么能做到这一点?(我这里优雅的主要标准:没有临时文件;没有忙碌等待或定期唤醒;没有异国情调的实用程序;尽可能短。)

bash shell-script background-process stdout stdin

95
推荐指数
6
解决办法
5万
查看次数

避免在 bash 中忙等待,没有 sleep 命令

我知道我可以通过执行以下操作来等待条件在 bash 中变为真:

while true; do
  test_condition && break
  sleep 1
done
Run Code Online (Sandbox Code Playgroud)

但它在每次迭代(睡眠)时创建 1 个子流程。我可以通过以下方式避免它们:

while true; do
  test_condition && break
done
Run Code Online (Sandbox Code Playgroud)

但它使用大量 CPU(忙等待)。为了避免子进程和忙于等待,我想出了下面的解决方案,但我发现它很难看:

my_tmp_dir=$(mktemp -d --tmpdir=/tmp)    # Create a unique tmp dir for the fifo.
mkfifo $my_tmp_dir/fifo                  # Create an empty fifo for sleep by read.
exec 3<> $my_tmp_dir/fifo                # Open the fifo for reading and writing.

while true; do
  test_condition && break
  read -t 1 -u 3 var                     # Same as sleep 1, but without sub-process.
done

exec …
Run Code Online (Sandbox Code Playgroud)

bash sleep shell-script

25
推荐指数
3
解决办法
3万
查看次数

标签 统计

bash ×2

shell-script ×2

background-process ×1

sleep ×1

stdin ×1

stdout ×1