Solved

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

Posted on 2014-01-14
4
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

688 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