Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

929 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

10 Experts available now in Live!

Get 1:1 Help Now