On 2011-03-10, Alexander Kapps <alex.ka...@web.de> wrote: > On 10.03.2011 23:25, Nobody wrote: >> On Thu, 10 Mar 2011 20:22:11 +0100, Giampaolo Rodol?? wrote: >> >>> I think he wants to attach to another process's stdin/stdout and >>> read/write from/to them. I don't know if this is possible but it >>> would be a great addition for psutil. >> >> It's not even a meaningful concept, let alone possible. > > Unless I misunderstand something, it is possible (at least on Linux):
Sometimes. [See below.] > Two terminal windows: > > 1: > alex@frittenbude:~$ grep foobar > > 2: > alex@frittenbude:~$ ps ax|grep 'grep foobar' > 13075 pts/4 S+ 0:00 grep --color=auto grep foobar > alex@frittenbude:~$ echo foobar > /proc/13075/fd/0 > > That this is *highly* system dependent, problematic in many regards > and just terrible hackery is another issue. That doesn't work for me: Terminal 1: $ grep foobar asdf Terminal 1: $ ps axf | grep "grep foobar" 7203 pts/4 S+ 0:00 \_ grep foobar 7205 pts/5 S+ 0:00 \_ grep grep foobar $ echo "asdf" >/proc/7203/fd/0 What the echo did was write to the tty device from which the "grep" is reading. The string "asdf" went directly to the tty. It didn't go to grep's stdin (grep wouldn't have displayed it). It _does_ work on processes who's stdin is an anonymous pipe: terminal window 1: $ (while sleep 1; do date; done) | grep foobar foobar asdffoobarqwer terminal window 2: $ ps axf | grep "grep foobar" 7229 pts/4 S+ 0:00 \_ grep foobar 7268 pts/5 S+ 0:00 \_ grep grep foobar $ echo "asdf" >/proc/7229/fd/0 $ echo "foobar" >/proc/7229/fd/0 $ echo "qwer" >/proc/7229/fd/0 $ echo "asdffoobarqwer" >/proc/7229/fd/0 We know that the data written to /proc/7229/fd/0 was going to grep's stdin, since lines with foobar were printed and lines without weren't. I'll do an 'echo "hi there" >/proc/fd/7346/fd/0 where 7346 is the pid of the instance of the "jed" text editor that I'm using to edit this post. The "hi there" string showed up in my terminal window, but it didn't actually get inserted in the file, because "jed" didn't see it. /proc/fd/7346/fd/0 points to /dev/pts/0. Writing to /dev/pts/0 sends data to the terminal window, _not_ to process 7346's stdin. Writing to /proc/fd/7346/fd/1 does the same thing. Writing to /proc/fd/7346/fd/2 sends the data to the tty where the write was done, since /proc/fd/7346/fd/2 points to /dev/tty, and /dev/tty points to different things depending on who's doing the writing. There are _some_ cases where you can write to /proc/<pid>/fd/0 and the program specified by <pid> sees the data on it's stdin. But, it doesn't work in many common cases. -- Grant Edwards grant.b.edwards Yow! Did an Italian CRANE at OPERATOR just experience gmail.com uninhibited sensations in a MALIBU HOT TUB? -- http://mail.python.org/mailman/listinfo/python-list