Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > attach to process by pid?

Reply
Thread Tools

attach to process by pid?

 
 
Danny Shevitz
Guest
Posts: n/a
 
      03-08-2011
Howdy,

Is there any way to attach to an already running process by pid? I want to send
commands from python to an application that is already running. I don't want to
give the command name to subprocess.Popen.

thanks,
Danny

 
Reply With Quote
 
 
 
 
Giampaolo RodolÓ
Guest
Posts: n/a
 
      03-10-2011
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.


--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/




2011/3/10 Miki Tebeka <(E-Mail Removed)>:
>> Is there any way to attach to an already running process by pid? I want to send
>> commands from python to an application that is already running. I don't want to
>> give the command name to subprocess.Popen.

> We probably need more information. What do you mean by "send commands"? (What was your work flow if you could give the command name to Popen?)
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      03-10-2011
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.

 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      03-10-2011
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):

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.

 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      03-10-2011
On Fri, Mar 11, 2011 at 8:55 AM, Alexander Kapps <(E-Mail Removed)> wrote:
> On 10.03.2011 23:25, Nobody wrote:
> Unless I misunderstand something, it is possible (at least on Linux):
>
> 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


This works fine on the Linux platform (perhaps other UNIX-like
platforms have similar concepts). You're simply writing to
the processes's stdin (file descriptor of 0)

Probably not a portable solution.

cheers
James

--
-- James Mills
--
-- "Problems are solved by method"
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      03-10-2011
On 2011-03-10, Alexander Kapps <(E-Mail Removed)> 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?
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      03-10-2011
On 2011-03-10, James Mills <(E-Mail Removed)> wrote:
> On Fri, Mar 11, 2011 at 8:55 AM, Alexander Kapps <(E-Mail Removed)> wrote:
>> On 10.03.2011 23:25, Nobody wrote:
>> Unless I misunderstand something, it is possible (at least on Linux):
>>
>> 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

>
> This works fine on the Linux platform


No it doesn't. Try writing something other than "foobar".

--
Grant Edwards grant.b.edwards Yow! An INK-LING? Sure --
at TAKE one!! Did you BUY any
gmail.com COMMUNIST UNIFORMS??
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      03-10-2011
On Fri, Mar 11, 2011 at 9:30 AM, Grant Edwards <(E-Mail Removed)> wrote:
> No it doesn't. ┬*Try writing something other than "foobar".


You've demonstrated a case where this doesn't work

cheers
James

--
-- James Mills
--
-- "Problems are solved by method"
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      03-11-2011
On Thu, 10 Mar 2011 23:55:51 +0100, Alexander Kapps 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,


You do ...

> it is possible (at least on Linux):
>
> 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


The /proc/<pid>/fd/<n> pseudo-files are just aliases for whatever "object"
(file, device, socket, ...) is attached to that descriptor. Writing to the
pseudo-file writes to the file, not to the descriptor. E.g.:

$ > foo.txt
$ sleep 100 < foo.txt &
[1] 17380
$ cat foo.txt
$ echo hello > /proc/17380/fd/0
$ cat foo.txt
hello

Similarly, if the process' standard input is a terminal, writing to
/proc/<pid>/fd/0 will write to the terminal. It will not cause the data to
appear on the process' standard input.

There are a few situations where it will work (for some values of "work").

E.g. in the case where standard input is a file, writing to .../fd/0 will
write to the file. If you open .../fd/0 in append mode (or if the process
hasn't started reading it yet), the next time the process reads from the
file, it will read the data which was just written. Of course, this
assumes that you have write permission on the file and don't mind
modifying it (and, if you *don't* use append mode, overwriting it).

Also, if .../fd/0 is a pipe (named or otherwise), opening it for write
will get you the write end of the pipe, while the process' standard input
has the read end. This is the one case which will usually work as expected.

Essentially, you can read or write files (pipes, devices, etc). You can't
read or write to another process' descriptors.

In the case of a pipe, writing to the pipe itself Does The Right Thing. In
some other cases, you could write to some other object to get the desired
result; e.g. if the process' stdin is a pseudo-terminal, writing to the
corresponding master device would cause the data to appear on the process'
stdin (but I don't know if that's actually possible with Unix98-style
ptys).

In other cases, there's no way to achieve the desired result. E.g. if the
process' stdin is a physical terminal, the only way to cause the
device to generate data is by pressing keys on the terminal.

 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      03-11-2011
On 11.03.2011 03:18, Nobody wrote:
> On Thu, 10 Mar 2011 23:55:51 +0100, Alexander Kapps 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,

>
> You do ...


Many thanks for the correction and lesson (to Grand Edwards too)!

I still try to digest your explanations. I thought, that processes
just do something like dup()'ing the file descriptors of their
terminal but after some strace experiments, I think that is totally
wrong.

I'd like to learn more about this (how processes, their controlling
terminals and the std file descriptors relate)

Do you know any links to deeper material (tried Google but what I've
found is to shallow)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-attach to process 'aspnet_wp.exe' on machine failed =?Utf-8?B?UmVzaG1hIFByYWJodQ==?= ASP .Net 3 11-13-2009 01:28 PM
Is there any shortcut for "Debug > Attach > Attach to aspnet_wp.exe"? Warren Tang ASP .Net 1 09-17-2008 03:46 PM
attach agent to applet running in firefox (attach api) craiget@gmail.com Java 0 06-14-2007 12:28 AM
debugger complains: auto-attach to process 'aspnet_wp.exe' =?Utf-8?B?c2lycGVsaWRvcg==?= ASP .Net 0 08-29-2005 04:41 PM
Auto-attach to process '[1084] aspnet_wp.exe' on machine <Machine =?Utf-8?B?UmVzaG1hIFByYWJodQ==?= ASP .Net 0 01-21-2005 05:33 AM



Advertisments