2

I am trying to write a script or a piece of code to archive files, but I do not want to archive anything that is currently open. I need to find a way to determine what files in a directory are open. I want to use either Perl or a shell script, but can try use other languages if needed. It will be in a Linux environment and I do not have the option to use lsof. I have also had inconsistant results with fuser. Thanks for any help.

I am trying to take log files in a directory and move them to another directory. If the files are open however, I do not want to do anything with them.

msw
  • 42,753
  • 9
  • 87
  • 112
Anthony
  • 21
  • 2

3 Answers3

3

You are approaching the problem incorrectly. You wish to keep files from being modified underneath you while you are reading, and cannot do that without operating system support. The best that you can hope for in a multi-user system is to keep your archive metadata consistent.

For example, if you are creating the archive directory, make sure that the number of bytes stored in the archive matches the directory. You can checksum the file contents before and after reading the filesystem and compare that with what you wrote to the archive and perhaps flag it as "inconsistent".

What are you trying to accomplish?

Added in response to comment:

Look at logrotate to steal ideas about how to handle this consistently just have it do the work for you. If you are concerned that rename of files will make processes that are currently writing them will break things, take a look at man 2 rename:

rename() renames a file, moving it between directories if required. Any other hard links to the file (as created using link(2)) are unaffected. Open file descriptors for oldpath are also unaffected.

If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another process attempting to access newpath will find it missing.

msw
  • 42,753
  • 9
  • 87
  • 112
  • I am basically trying to take log files in a directory and move them to another directory. If the files are open however, I do not want to do anything with them. – Anthony May 31 '11 at 16:08
  • @Anthony: why? can you explain? the best answer is to procedurally know which files are being written to... – ysth May 31 '11 at 16:50
1

Try ls -l /proc/*/fd/* as root.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • 1
    this will only tell you what was open when you ran the command, not what is open now or has been opened and closed since. – msw May 31 '11 at 15:59
  • 1
    No, `/proc/*/fd` does get updated as the process opens and closes file descriptors. – mob May 31 '11 at 16:10
  • 2
    and in the delay between reading `/proc` and opening the file, anything can happen. – msw May 31 '11 at 16:18
  • These comments are not constructive. Why not offer a better solution when criticising the proposed one? – Werner Jun 07 '12 at 13:23
1

msw has answered the question correctly but if you want to file the list of open processes, the lsof command will give it to you.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169