When is /bin/sync ever useful in modern linux?

The sync utility is a trivial wrapper around the sync system call.

You don't need to call it explicitly before shutting down the computer… because the shutdown scripts do it for you! Actually, that's not necessary in most cases, because unmounting a filesystem, or switching it to read-only, flushes the data of that filesystem to the disk, and the shutdown scripts do that. Calling sync is a belts-and-braces thing, in case something goes wrong and a process somehow survives the shutdown sequence and prevents unmounting.

sync is also called at the next-to-last stage of a suspend or hibernation sequence, just before powering off the hardware. Here there's no alternative, something has to say “write all data to disk now”.

Another time when sync is useful is before doing something that has a risk of causing a crash, e.g. trying out an experimental driver.

Calling sync before a program crashes is useless. sync handles data in buffers between the programs and the storage media; it doesn't do anything to data that a program hasn't saved to a file.

Calling sync is also useless when examining available memory, since it doesn't affect available memory. While it does free write buffers for reclamation, it doesn't actually free those buffers — they'll be freed when the kernel needs to allocate memory for something else, in the meantime they stay around as cache.

Tags:

Linux