Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Finding out by one process a filename opened by another process.

by humble (Acolyte)
on Aug 01, 2012 at 09:41 UTC ( #984742=perlquestion: print w/replies, xml ) Need Help??
humble has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for PERL right way decision regarding finding out by one process a fullpath to a file opened by another process.

I have a PERL script w/ several BASH code insertions. I want to rewrite some BASH code in PERL - hence this question.

This script waits (w/ the help of Term::ReadKey module) for key pressings, - undertaking file processing (say, file removal from FS). The full path to that file now is cathed as follows:

$tmp=qx#/bin/ps -AHf#;
regexp for PID
$tmp=qx#/bin/cat /proc/$1/cmdline#;
regexp for a full path to the file (to avoid some encodings troubles)

from a forked process being started w/

system( "/usr/bin/mkfifo $pipe_file && $script $pid $params &" );

- though not the best solution regarding PERL, but worked - apposed to PERL's fork function - haw pipe behaved wrong in some circumstances.

Finally, my question is, What's the best PERL practics - catching full path to a filename, opened now by one process and cacthed another? F.e.: process A opened a file, how process B can get that file full path? - Both pocesses are run on same host, under same user. Both proccess has pipe filename, or may have an ID - thus identifying itself having relation one w/ another.

Thank You for Your time and wisdom.

  • Comment on Finding out by one process a filename opened by another process.

Replies are listed 'Best First'.
Re: Finding out by one process a filename opened by another process.
by Jeppe (Monk) on Aug 01, 2012 at 10:21 UTC

    If you have the PID, then the shell command lsof -p will list all files opened by that PID. Also, lsof <filename> will list the processes that have opened a specific file. Etc.

    lsof is a very powerful command, and it requires root access, so you'll have to provide password-less sudo access for the user executing the script in order for this solution to work.

      Upon further consideration, it looks to me like you might want to simply build a perl package with system constants, or use a system-wide configuration file, depending upon what precisely your application does.

      Using shared memory is also an option if the name of the pipe will change, the server application will then write the filename of the pipe to shared memory with a pre-determined shared memory key, and the client will then read the value from shared memory. IPC::ShareLite may be all you need.

        Another approach is to write the filename (w/ its path) to a file - by forked process, then another process (waiting for the keypress) will simply read the file and get the filename w/ its path. - But I do like it because app. optimimum - that I prefer - forked process will write each file as it processes it, where waiting for key press - only now and then will use the file.
      Here (w/ my question) I pursue 2 goals:

      1. move part of my code written in BASH to PERL

      2. avoid "wide characters".


      1. I'm lookign for PERL technique - how to implement this in PERL. I have done that already using BASH or OS tools, though.

      2. I'm boring w/ wide characters problem - it just makes me seek. - The more I try to go PERL way, the more problems I get - that seems to me unresolvable. F.e.: trying to fix it PERL way - I have to module Encode w/ its functions en/de -code - the great and unresolvable problem here is that I have to guess right encoding - and no automation - of course on a batch processing script like mine - it is hardly implemetable solution. - At other hand, using BASH/OS tools incapsulations to my PERL code, blesses me w/ no such a problem - a work around have been found in opening /proc/PID/cmdline - that has the filename and its path in purity - allowing me to access the file farther by PERL code.

      So, having said that, I'm wondering if it is the way to go? Or there some techniques that I'm not aware of, and I can use PERL for my situation?

Re: Finding out by one process a filename opened by another process.
by moritz (Cardinal) on Aug 01, 2012 at 10:11 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984742]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-05-26 17:17 GMT
Find Nodes?
    Voting Booth?