检查USB设备是否空闲,LINUX [英] Check if USB device is idling, LINUX

查看:24
本文介绍了检查USB设备是否空闲,LINUX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的问题,但我找不到答案.是否可以在 linux(或 python)中查看外部 USB 笔式驱动器是否空闲?对于我正在编写的 python 脚本,我需要知道这一点.一旦没有写入任何内容,我需要重命名外部 USB 笔式驱动器上的文件夹.

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.

我知道有 lsof 命令可以列出打开的文件.'lsof/theDir' 只能工作一半.当复制到 USB 的过程仍在运行时,它工作正常.但是当进程停止时, lsof 什么也不显示.但操作系统仍在从其缓冲区写入 USB.

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.

推荐答案

您可以通过查看 /sys/block//stat.第九列包含当前进行中的 I/O 数量.检查 https://www.kernel.org/doc/Documentation/block/stat.txt
一旦这个数字为零,设备应该是空闲的.

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.

要强制立即写入所有缓冲区,您可以执行 sync 并等待它返回.

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.

这篇关于检查USB设备是否空闲,LINUX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆