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

前端 未结 1 1766
予麋鹿
予麋鹿 2021-01-14 04:12

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 ano

相关标签:
1条回答
  • 2021-01-14 04:27

    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

    0 讨论(0)
提交回复
热议问题