Solved

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

Posted on 2014-01-14
4
106 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now