4

I've got a quick question, but I can't find an answer. Is it possible in linux (or in python) to see if an external usb pen drive is idling? I need to know this for a python script I'm writing. I need to rename a folder on an external usb pen drive as soon as nothing is writing to it.

edit: I know there is lsof command to list open files. 'lsof /theDir' only works half. It works OK when the process copying to the USB is still running. But when the process stops, lsof shows nothing. But the OS is still writing to the USB from its buffer.

Bosiwow
  • 2,025
  • 3
  • 28
  • 46
  • But you will have a race: you check, the USB is idle, then you are going to rename the folder, but something goes faster and starts doing something. Then you do the rename in a non-idle state. You would need something like a volume/directory lock. Maybe unmounting the volume and mounting elsewhere in a private directory? – rodrigo Jul 29 '14 at 08:19
  • as far as I understand, Linux can handle this case. I just need to make a backup from a usb drive to another. I start creating a folder "incomplete files" at the destination. After that, I start copying. When the copying is done and I've got all my files, I want to rename that folder to complete+currentDate. – Bosiwow Jul 29 '14 at 08:26

1 Answers1

6

You can check if all I/O has been processed by having a look at /sys/block/<dev>/stat. The ninth column contains the number of I/Os currently in flight. Check https://www.kernel.org/doc/Documentation/block/stat.txt
Once this numner is zero the device should be idle.

To force all buffers to be written immediately you could execute sync and wait until it returns.

Nevertheless be aware that you have a race condition here if you are not controlling the writing - after you decided that the device is idle some other process could start writing to it.

Yourstruly
  • 600
  • 5
  • 12
  • Yeah, I've got a raspberry pi and if I use sync the transfer is about 400 KB/s, with the fat flush option enabled I get around 5 megabytes/s. – Bosiwow Jul 29 '14 at 08:28
  • Isn't there something to check if the writing is done without using sync? – Bosiwow Jul 29 '14 at 08:29
  • You don't need to use sync to do a backup, the OS will handle that automatically. – rodrigo Jul 29 '14 at 08:35
  • 1
    Updated my answer - /sys/block//stat might be what you are looking for. – Yourstruly Jul 29 '14 at 08:42
  • thanks alot! Is there a way to do a manual sync command? I've been looking for this but I couldn't find it. Can I do just sudo sync()? Because I've read that this is always succesful. How can I be sure it worked then ? http://man7.org/linux/man-pages/man2/sync.2.html – Bosiwow Jul 29 '14 at 08:52
  • 1
    Hm, I think we have a misunderstanding here when we talk about sync. I am not talking about mounting the filesystem in sync mode. I was talking about executing the command sync. You can even call this as non-root user without sudo. Once returned you can be relatively sure that your copy operation is done. See http://man7.org/linux/man-pages/man1/sync.1.html – Yourstruly Jul 29 '14 at 09:02
  • ooow I see, So I don't put any parameters behind it? Just sync ? – Bosiwow Jul 29 '14 at 09:08