Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Easy way to check if a file is open needed.

by haukex (Archbishop)
on Apr 08, 2019 at 20:18 UTC ( [id://1232317]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: Easy way to check if a file is open needed.
by Apt_Addiction (Novice) on Apr 08, 2019 at 23:21 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found