I have a python script that needs to output some values to stdin and copies another string to clipboard. I'm using the module subprocess to execute the xclip utility through Popen something like this:
# clip.py
import subprocess
from subprocess import Popen
print('PRINT_ME')
p1 = Popen(['xclip', '-selection', 'clipboard'], stdin=subprocess.PIPE)
p1.communicate(input=('PASTE_ME'.encode()))
The script works as intended: PRINT_ME is echoed in bash and PASTE_ME is available to paste, and returns imediatly.
A problem appears when the script output is piped to another command. Suppose one wants to redirect to a file and stdin using tee:
$ python clip.py|tee file.txt
The program works as supposed but not returns, i.e. shell don't give control back.
How can this could be fixed?
Some important info: the xclip utility forks itself (to cope with implementation of clipboards on X) maintaining the string avaliable to copy, when used in shell it returns immediately (it forks to background). It appears that the shell is attaching both clip.py and xclip stdin/stdout to tee.
If xclip -selection clipboard is found using ps u and killed the command returns.
I'm using Python 3.4.
Thanks