Solved

Is there a file descriptor created for logfile using by logmsg within Perl script

Posted on 2014-01-14
4
127 Views
Last Modified: 2016-06-07
We have a log file which is updated by one Perl script using logmsg.  But I find nothing when I am using lsof|grep logfile to try to find out the process id. Actually I already know which process but I also did not find this logfile descriptor in /proc/processid/fd.  Why?  What is the right way to find out this process ?
0
Comment
Question by:c11v11
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39781324
How is logmsg defined?
0
 

Author Comment

by:c11v11
ID: 39784438
sub logmsg {
    my ($msg) = @_;

    my $LOG = new IO::File::fcntl("$logFile",'a','lock_ex') or warn "$!";
    my $DateTime = strftime "%b %e %Y %T", localtime;
    if ($LOG) {
        print $LOG "$DateTime,$msg";
        print "$DateTime,$msg" if (! $daemon);
        close $LOG;
    }
}

How can I get the $LOG value? Is this the value for file descriptor?
0
 
LVL 3

Accepted Solution

by:
bigbed earned 40 total points
ID: 40108719
The file is only opened very briefly while writing an individual log message.
The chances of you running lsof at exactly the same time as it's writing a log message are slim.

In the logmsg() function the $LOG variable is scoped so that it's not visible outside that function.
You could either add some code to do what you want to do within the
if ($LOG){} block
or change the scope of $LOG (have a "my $LOG" or "our $LOG" at a higher level in the program and adapt logmsg() to keep the file open and check for the file already being open when it's called).

Here's a rough example
my $LOG;
..
..
sub logmsg {
  my ($msg) = @_;
  $LOG ||= new IO::File::fcntl("$logFile",'a','lock_ex') or warn "$!";
  my $DateTime = strftime "%b %e %Y %T", localtime;
  if ($LOG) {
    print $LOG "$DateTime,$msg";
    print "$DateTime,$msg" if (! $daemon);
  }
}

Open in new window


good luck,
Tim
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 41640814
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: bigbed (http:#a40108719)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

suhasbharadwaj
Experts-Exchange Cleanup Volunteer
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question