Andres
Andres

Reputation: 11757

How can I check if a file is being used by other application?

I need to process a video file, and I need the file to be completed before I open it. So I need to check if the file is opened or not before processing it, but opened by another process. Any idea how to check this? I'm using Linux.

Upvotes: 5

Views: 1491

Answers (1)

Faiz
Faiz

Reputation: 16265

Without any additional gems, a slightly wasteful way might be:

if %x[lsof -F n].split("\n").grep(/yourfilename/).empty?
  # all clear
else 

end

Or

if system %Q[lsof #{filename}] 
  # still open..
else 
  # all clear
end

Or, ignore my hacked suggestion and use a gem for this: https://github.com/jordansissel/fosl

Upvotes: 8

Related Questions