3

I have a shell script which archives log files based on the whether the process is running or not. If the log file is not used by the process then I archive it. Until now, I'm using lsof to get the log file being used but in future, I have decided to use perl to do this function.

Is there a perl module similar to what lsof in linux can perform ?

cbrdy
  • 792
  • 1
  • 8
  • 27

2 Answers2

4

There is a perl module, which wraps around lsof. See Unix::Lsof.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • is there any other module other than Unix::Lsof? This function again depends on lsof binary which I'm going to uninstall in the near future. – cbrdy Jun 19 '12 at 21:18
  • @nlrreddy: Why are you uninstalling it? At least on my system, the `lsof` package weighs in at a whopping 0.9 megabytes. – Daenyth Jun 19 '12 at 21:59
  • @Daenyth: I have seen issues where lsof wouldn't work after os patch. – cbrdy Jun 20 '12 at 16:33
2

As I see it, the big problem with not using lsof is that one would need to work in a way that is independent of the operating system. Using lsof allows the perl programmer to work with a consistent application allowing for operating system independence.

To have a perl module developer to write lsof would, in effect, be writing lsof as a library and then link that into perl - which is much more work than just using the existing binary.

One could also use the fuser command, which shows the process IDs with the file handle. There is also a module which seeks to implement the same functionality. Note from the perldoc:

The way that this works is highly unlikely to work on any other OS other than Linux and even then it may not work on other than 2.2.* kernels.

One might try walking /proc/*/fd and looking at the file descriptors in there to see if any are pointing to the file in question. If it is known what the process ID of a running process that would be opening the log file, it would be just as easy to look at that process. Note, that this is how the fuser module works.

That said, it should be asked "why do you want to move away from lsof"?