bash: pipe data into an exec'd command

2019-08-02 18:58发布

问题:

I have a program, sink.py, that I want to run in a bash script (run.sh). sink.py needs data from stdin to do its job.

I'd like to feed data from another program, source.py, into sink.py. The following pipeline works fine:

python source.py | python sink.py

However, I'm looking to exec sink.py, so that it takes over the shell. I want to do this because I'm starting this bash script from a process manager (supervisord) and sink.py has all the signal handling code. So ideally I could do something like this:

python source.py | exec python sink.py

But this doesn't seem to work -- ps shows that run.sh continues to run as a parent of sink.py.

I tried using process substitution like this:

exec python sink.py < <(python source.py)

This is almost perfect, except the bash script appears to become a "zombie", which presumably is not a good thing:

 PID TTY      STAT   TIME COMMAND
1042 pts/7    S      0:00 python sink.py
1046 pts/7    Z      0:00 [run.sh] <defunct>

One other thought I had was to just save the output of source.py into a temporary file, then direct the file into sink.py. That almost works, except I have no way of deleting the temp file once I exec sink.py.

Is there any good way to accomplish this?

标签: linux bash shell