Linux 如何从命令行刷新硬盘和闪存盘(或文件系统)的缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2384202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to flush cache of hard-disk and flash-disk (or filesystem) from command line?
提问by Vikrant Chaudhary
I'm writing a backup script which
我正在写一个备份脚本
- Copies the data to backup disk.
- Flushes the backup disk.
- Performs a hash integrity check.
- 将数据复制到备份磁盘。
- 刷新备份磁盘。
- 执行哈希完整性检查。
Before I used to do sleep(60)
for waiting a minute so that data is automatically flushed by the kernel. Which I guess is overkill so now I'm trying sudo hdparm -F --verbose /dev/disk
but it reports error - HDIO_DRIVE_CMD(flushcache) failed: Invalid exchange
multiple times.
以前我经常做sleep(60)
等待一分钟,以便内核自动刷新数据。我想这有点矫枉过正,所以现在我正在尝试,sudo hdparm -F --verbose /dev/disk
但它HDIO_DRIVE_CMD(flushcache) failed: Invalid exchange
多次报告错误。
I'm wondering is there any standard way to flush the cache to hard disk. I think there is because usb-creator-gtk
does it, umount
does it.
我想知道是否有任何标准方法可以将缓存刷新到硬盘。我认为这是因为usb-creator-gtk
它,umount
它。
I'm using Ubuntu x64 9.10
我正在使用 Ubuntu x64 9.10
PS: I'm trying to avoid "sync"because this page says that it is notsafe. http://ubuntuforums.org/showthread.php?t=589975
PS:我试图避免“同步”,因为这个页面说,这是不是安全的。http://ubuntuforums.org/showthread.php?t=589975
采纳答案by Duck
Does sync
suffice?
是否sync
足矣?
Edit:regarding your edit - you are trying to avoid sync because some guy on the internet put a CYA disclaimer on his post? Maybe there is something wrong with sync of which I am unaware but that might be worth a 2nd post in itself.
编辑:关于您的编辑 - 您试图避免同步,因为互联网上的某个人在他的帖子上发表了 CYA 免责声明?也许我不知道同步有问题,但这本身可能值得发第二篇文章。
Still, from the linux info pages:
尽管如此,从 linux 信息页面:
sync writes any data buffered in memory out to disk. This can include (but is not limited to) modified superblocks, modified inodes, and delayed reads and writes. This must be implemented by the kernel; The sync program does nothing but exercise the 'sync' system call.
同步将内存中缓冲的任何数据写入磁盘。这可以包括(但不限于)修改的超级块、修改的 inode 和延迟的读取和写入。这必须由内核实现; 同步程序除了执行“同步”系统调用之外什么都不做。
回答by George
You want fsync (man section 2) function call, but if you're doing this in a script, you'll probably want to use the sync command (man section 8), which just calls sync().
您需要 fsync (man section 2) 函数调用,但如果您在脚本中执行此操作,您可能想要使用仅调用 sync() 的 sync 命令(man section 8)。