in reply to Easy way to check if a file is open needed.

Thanks for your replies.

Sorry I wasn't clear about the purpose. It's just a hobby and I'm trying to learn and solve the puzzle. There's probably alsorts of bad in this, but here goes...

Wife has a USB stick with her work on, and she gets files from co-workers. They need backing up to the cloud in case of loss/forgeting stick. So, I have rsync running on a crontab that copies her USB stick contents into OneDrive. All works well until someone puts an illegal filename into the mix. I don't get to use her computer much 'cos she's nearly always using it. So I want to run a script as a crontab to clean up any issues and stop things from jamming up.

If she's got a file open in MS Word, I don't want it renamed, like it does at present. So I'm wanting to check for file handles in the tree whilst it's being used. When she closes the file, the script will run again at some point and take care of the issues.

Thanks again, and I hope that makes it a bit clearer.

  • Comment on Re: Easy way to check if a file is open needed.

Replies are listed 'Best First'.
Re^2: Easy way to check if a file is open needed.
by haukex (Archbishop) on Apr 08, 2019 at 20:18 UTC
    I have rsync running on a crontab that copies her USB stick contents into OneDrive.

    By this, do you mean that rsync is basically just doing a local copy from the USB stick to whatever local folder OneDrive is syncing to the cloud? If you don't want to do the rename directly on the USB stick: I'm not sure whether OneDrive would tolerate this, but perhaps you could do the rename directly inside the OneDrive directory immediately after the rsync? Another option might be an intermediate staging area, i.e. USB stick --rsync--> staging area, do rename here --rsync--> OneDrive folder; although that's of course less efficient, if it's not much data, it may be fine.

    If you do want to do the rename on the USB stick: At the moment I'm not aware of any better method of checking if specific files are open than lsof (or similar tools such as fuser), which is probably not particularly efficient.

      Thank you for that - really helpful. Yes, rsync just updates OneDrive from USB every 10 minutes or so. I'd got so wrapped up in testing this outside of OneDrive, that I hadn't thought of some of the things you mention. Looking at my OneDrive folder with lsof, OD has handles on files that aren't open anywhere else. So, your idea of renaming on the USB stick is best. Plus, she has quite a lot of data.

      In terms of the time and things happening whilst the script runs, I have 1,200 files in my OD and this script found and renamed about 40 in 0.12 seconds, which seems OK. The time hog is lsof, which takes about 13 seconds to run. This seems ridiculous compared to linux, but apparently it's a mac thing.

      Anyway, I'll create a temp file with the lsof output for the tree, then grep for the current filename before proceeding further. I understand grep will return 0 if it finds something.

        The time hog is lsof, which takes about 13 seconds to run. ... I understand grep will return 0 if it finds something.

        Note that it's possible to get lsof to only look in one directory tree with its +D option, although the manpage does note that it can consume a fair amount of memory, so you might want to keep an eye on that in testing.

        As for running external programs from Perl, I've written about that a fair amount, e.g. here - in this case, it seems like capturex from IPC::System::Simple might be a good replacement for qx// (backticks) - if the processes you're running are expected to return either 0 or 1 and that's not an error, then you can specify [0,1] as the first argument.