in reply to How to know STDOUT associated with a process pid

For the simplest approach (on a *NIX box), you can determine your own PID ($$) and hence you can determine a more detailed view of your process details using ps -fp <PID> and subsequently parse the filename off the end e.g. (untested) (my $fname = `ps -fp $$`) =~ s/.*>\s+//;

This may not work reliably if the command entered on the CLI was excessively long - the filename being written to via STDOUT may disappear off the end of the line - however, some *versions of ps(1) support the production of a wider field e.g. -ffp, -fwp etc.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: How to know STDOUT associated with a process pid
by MidLifeXis (Monsignor) on Feb 09, 2009 at 15:02 UTC

    Update: Moved hedges from comments to here to aid in reading. This set of comments is entirely from my memory of *nix variants I have used and working through similar problems. There may be other / newer versions of *nix and ps that differ from my memory.

    PS does not display the part of the command line after a pipe or redirect symbol, because it is not a part of the process - it is only a method of setting up the file descriptors in whatever program is calling the programs. The information after the redirection or pipe is another process or file, and would not be stored or shown in the process table.

    The command foo | bar > biz would be parsed so that foo's STDOUT would be hooked up to bar's STDIN, and bar's STDOUT would be attached to the file handle attached to the file biz. Two different processes. Unless the shell (the one spawning the foo | ... commands) rewrites its $0 to reflect what is currently being written (which may be the case), the entire tubing between programs is not displayed using the ps command.

    Now, that being said, I would like to be proven wrong, because there are a number of instances where that would be quite useful. :-)

    --MidLifeXis