bgupta has asked for the wisdom of the Perl Monks concerning the following question:

I have a requirement of getting the STDOUT associated with a process pid, e.g If I do
sample_prog.pl > /my/working/dir/filename.txt
Inside the above 'sample_prog.pl' ,I can get the associated pid but can I know somehow the path of the output file 'filename.txt'? Please help!!

Replies are listed 'Best First'.
Re: How to know STDOUT associated with a process pid
by cdarke (Prior) on Feb 06, 2009 at 17:22 UTC
    On Linux the /proc/pid/fd sub-directory for fd 1 will give this info (but the UNIXs I have looked at do not).
Re: How to know STDOUT associated with a process pid
by Anonymous Monk on Feb 06, 2009 at 16:02 UTC
Re: How to know STDOUT associated with a process pid
by MidLifeXis (Monsignor) on Feb 06, 2009 at 16:02 UTC

    Perhaps something with fstat (see POSIX for information) could be worked up? According to my man page for the system version of fstat, you can get the device and inode of a specific file descriptor. Perhaps you could then use that with some of the file test operators to find it on the file system.

    Be aware, however, that just because you have an inode does not mean that it is linked into the file system. If you open a file, and then unlink it, you can still write to the file pointed to by that inode without it being available from the file system.

    --MidLifeXis

      If I am understanding it correctly this would require a file descriptor of the output file, since I am invoking the perl code from unix command line or a shell script , how can I get the file descriptor of the filename which will be created by the redirected output of the invoked perl program inside that perl program ? The perl script has been invoked via shell scripts at several places and I am going to modify the perl script to know where its output is being redirected by invoking it through unix command line( or shell script ). Please clarify if I am missing something.

        STDOUT can get you a file descriptor of some sort, right?

        It has been many years since I have done this, but the -t test in the shell can tell if it is attached to a file or a pty, so I would guess that you can get to the inode (if applicable) attached to the STDOUT as well.

        Some of the other responses on this posting also have good ideas, some even with already written solutions (even the negative responses have some good stuff in them if you dig a little). I would pursue the fstat stuff only if the other stuff on this thread didn't pan out. In fact, it would not surprise me if some of the other solutions even used fstat behind the scenes.

        Good luck

        --MidLifeXis

Re: How to know STDOUT associated with a process pid
by Bloodnok (Vicar) on Feb 07, 2009 at 00:53 UTC
    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 :-))

      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