Capture the stdout of another process.

  • Follow


Hi,
Can we capture the stdout of another process, given only the pid of
that running process ?
Thanks in advance,
Gopu.
0
Reply gopugopu 10/15/2003 2:38:35 PM

gopugopu@rediffmail.com (Gopu Bhaskar) writes:

> Can we capture the stdout of another process, given only the pid of
> that running process ?

It's possible but not trivial.  I did a little experiment and ran
"cat", without any arguments.  I attached to that cat process with gdb
and redirected its stdout to a file.  I suppose you could do use named
pipes to read the output in a different process.  Still, there might be a
much better way, for all I know.

What exactly is it you want to accomplish?

-- 
M�ns Rullg�rd
mru@users.sf.net
0
Reply mru 10/15/2003 2:47:37 PM


gopugopu@rediffmail.com (Gopu Bhaskar) writes:

> Can we capture the stdout of another process, given only the pid of
> that running process ?

If it's running in an X environment I believe there's some way of
getting the server to give you the i/o of a window, or so I've been
told. 

If that's the case, try asking this in an X programming newsgroup.

Joe
0
Reply joe 10/15/2003 4:23:16 PM

mru@users.sourceforge.net (M�ns Rullg�rd) wrote in message news:<yw1x7k36sgl2.fsf@users.sourceforge.net>...
> gopugopu@rediffmail.com (Gopu Bhaskar) writes:
> 
> > Can we capture the stdout of another process, given only the pid of
> > that running process ?
> 
> It's possible but not trivial.  I did a little experiment and ran
> "cat", without any arguments.  I attached to that cat process with gdb
> and redirected its stdout to a file.  I suppose you could do use named
> pipes to read the output in a different process.  Still, there might be a
> much better way, for all I know.
> 
> What exactly is it you want to accomplish?

In the system in which I am working on many processes are started at
bootup. The printfs output dont come into any console. I was planning
to write a program which would capture the outputs of those printfs to
our console window.

something like `capture <pid>`, on running this we should be able to
see the printf outputs of the process with id pid.

Hope I am clear.
0
Reply gopugopu 10/16/2003 7:22:28 AM

gopugopu@rediffmail.com (Gopu Bhaskar) writes:

> mru@users.sourceforge.net (M�ns Rullg�rd) wrote in message
> news:<yw1x7k36sgl2.fsf@users.sourceforge.net>... 
> > gopugopu@rediffmail.com (Gopu Bhaskar) writes:
> > 
> > > Can we capture the stdout of another process, given only the pid
> > > of that running process ?
> > 
> > It's possible but not trivial.  I did a little experiment and ran
> > "cat", without any arguments.  I attached to that cat process with
> > gdb and redirected its stdout to a file.  I suppose you could do
> > use named pipes to read the output in a different process.  Still,
> > there might be a much better way, for all I know.
> > 
> > What exactly is it you want to accomplish?
> 
> In the system in which I am working on many processes are started at
> bootup. The printfs output dont come into any console. I was
> planning to write a program which would capture the outputs of those
> printfs to our console window.

If you have control over the boot process, just wrap the executables
in scripts which redirect the output before exec-ing the
programs. Then call the wrapper scripts from inittab or wherever.

Joe
0
Reply joe 10/16/2003 1:36:44 PM

Gopu Bhaskar wrote:

> mru@users.sourceforge.net (M�ns Rullg�rd) wrote in message news:<yw1x7k36sgl2.fsf@users.sourceforge.net>...
> 
>>gopugopu@rediffmail.com (Gopu Bhaskar) writes:
>>
>>What exactly is it you want to accomplish?
> 
> 
> In the system in which I am working on many processes are started at
> bootup. The printfs output dont come into any console. I was planning
> to write a program which would capture the outputs of those printfs to
> our console window.
> 
> something like `capture <pid>`, on running this we should be able to
> see the printf outputs of the process with id pid.
> 
> Hope I am clear.

You might want to try using a system call tracer like strace or truss 
(depending on the system) to do this.  You could wrap it in an awk 
script if you need a particular format.

Having said that, if you have any control over development of those 
programs you should have them use syslog rather than writing to stdout. 
  Syslog is the utility specifically designed to allow daemons to 
report information in the form you are looking for.  It also gives you 
considerable configuration control over where the logged information 
goes.  See the man pages for syslog, syslogd and syslog.conf

-- ced


-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

0
Reply Chuck 10/16/2003 2:02:09 PM

On Thu, 16 Oct 2003 13:36:44 GMT
joe@invalid.address wrote:

> gopugopu@rediffmail.com (Gopu Bhaskar) writes:
>=20
> > mru@users.sourceforge.net (M=E5ns Rullg=E5rd) wrote in message
> > news:<yw1x7k36sgl2.fsf@users.sourceforge.net>...=20
> > > gopugopu@rediffmail.com (Gopu Bhaskar) writes:
> > >=20
> > > > Can we capture the stdout of another process, given only the pid
> > > > of that running process ?
> > >=20
> > > It's possible but not trivial.  I did a little experiment and ran
> > > "cat", without any arguments.  I attached to that cat process with
> > > gdb and redirected its stdout to a file.  I suppose you could do
> > > use named pipes to read the output in a different process.  Still,
> > > there might be a much better way, for all I know.
> > >=20
> > > What exactly is it you want to accomplish?
> >=20
> > In the system in which I am working on many processes are started at
> > bootup. The printfs output dont come into any console. I was
> > planning to write a program which would capture the outputs of those
> > printfs to our console window.
>=20
> If you have control over the boot process, just wrap the executables
> in scripts which redirect the output before exec-ing the
> programs. Then call the wrapper scripts from inittab or wherever.
Many Unix/Linux systems use startup scripts usually located in
/etc/init.d or /sbin/init.d or /etc/rc.d/init.d. These scripts also may
set up how these daemons log their messages. And some are just started
with output redirected to /dev/null. And, some do write to the console.
You may even be able to grab some of the output via the dmesg(8)
command.=20

But, as M=E5ns mentioned, if you really need to capture the the output of
an already running program, it is non-trivial and non-portable.=20

--=20
Jerry Feldman <gaf-nospam-at-blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
0
Reply Jerry 10/16/2003 2:11:11 PM

joe@invalid.address wrote in message news:<m3brshz4my.fsf@invalid.address>...
> gopugopu@rediffmail.com (Gopu Bhaskar) writes:
> 
> > mru@users.sourceforge.net (M�ns Rullg�rd) wrote in message
> > news:<yw1x7k36sgl2.fsf@users.sourceforge.net>... 
> > > gopugopu@rediffmail.com (Gopu Bhaskar) writes:
> > > 
> > > > Can we capture the stdout of another process, given only the pid
> > > > of that running process ?
> > > 
> > > It's possible but not trivial.  I did a little experiment and ran
> > > "cat", without any arguments.  I attached to that cat process with
> > > gdb and redirected its stdout to a file.  I suppose you could do
> > > use named pipes to read the output in a different process.  Still,
> > > there might be a much better way, for all I know.
> > > 
> > > What exactly is it you want to accomplish?
> > 
> > In the system in which I am working on many processes are started at
> > bootup. The printfs output dont come into any console. I was
> > planning to write a program which would capture the outputs of those
> > printfs to our console window.
> 
> If you have control over the boot process, just wrap the executables
> in scripts which redirect the output before exec-ing the
> programs. Then call the wrapper scripts from inittab or wherever.
> 
> Joe


The system on which i am working cannot be rebooted. Seems like
filtering the truss output would be the best option. I will continue
investigting....
0
Reply gopugopu 10/20/2003 8:01:15 AM

7 Replies
491 Views

(page loaded in 0.283 seconds)

Similiar Articles:













7/20/2012 3:17:09 PM


Reply: