【发布时间】:2018-10-05 10:54:31
【问题描述】:
场景是我需要我的主命令在当前 shell 中运行,这是必需的或丢失所有环境内容等。
所以,我不能这样运行我的管道:
#command-line 1
mainCommand | (
...subshell commands...
) &
#this wait works, but main command is in child process
wait $!
我必须在当前 shell 中运行主命令:
#command-line 2
mainCommand &> >(
...subshell commands...
) &
#this wait is waiting for mainCommand, not subshell
wait $!
但是,在命令行 2 中,它只是一个命令,我不能将其发送到后台,只有 subshell 应该进入后台,然后我才能获取它的 PID。
如何让
- 主命令在当前shell中
- “wait”命令实际上是在等待子 shell 吗?
我有锁定文件的解决方案,但我不喜欢使用文件,因为整个脚本不断运行,一次又一次地写入/修改文件就像穿透文件系统。
【问题讨论】:
-
你有理由相信
mainCommand会在子shell之后运行很长时间吗?一旦 subshell 退出,mainCommand将在尝试写入现已关闭的管道时立即退出。 -
在
bash5,您将可以等待进程替换;mainCommand > >(...); wait会做你想做的事。 -
是的,期待已久的 bash 5
标签: bash pipe wait pid subshell