5

I'm curious why /bin/sync exists. I understand what it does, why paging happens and why it's important that an operating system sync. However, I'm still failing to see when calling /bin/sync is useful.

It seems to me that Linux must already sync whenever anything important is going to happen (such as before a shutdown, the lack of which in proto-Linux lead to the sync; sync; sync; myth). If Linux is going to be reliable, and we all seem to believe it is, then I must assume it's going to ensure blocks are written to the FS before it runs a command that would harm the OS without a sync.

The sync(8) man page suggests that sync be run when a program is going to close in an improper manner. I sort of see this, though I think only a very tiny number of programs would have reason to use sync (and wait around to make sure the suggestion worked), but wouldn't be able to close through a cleaner method that lets the OS sync for them.

In any case sync(8) is not the same as the /bin/sync binary. I think I must be missing a situation where the binary is useful? The only use case I've seen is running it right before making a call to mem to free up more memory and perhaps give a slightly better idea of you 'available' memory. It would be useful if you somehow realized your computer was going to explode and had just enough time to type sync before it happened perhaps, but no one is doing that by hand, instead of some watchdog program calling sync(8).

Are there other advantages to the binary I'm missing?

  • 1
    At a guess, it'll be because it's part of POSIX – Sobrique Dec 31 '15 at 22:00
  • @Sobrique looking back at this question after I realized I never selected an answer for it. I see why, while all of the answers give good information I don't feel any really give a good reason for sync existing today. In all honesty I'm feeling like your offhand comment really is the best explanation, legacy standard that's not really required any more but needs to be maintained. If this was an actual answer I would be tempted to pick it :) – dsollen Feb 3 '16 at 19:26
0

"history of sync" is a tough thing to google for ;-) A totally uninformed guess is that at least one legitimate use is for kernel developers making changes to the sync(2) system call to test. They need a way to invoke the syscall that's a little more convenient than halting or umounting a filesystem?

  • picking this because it's the only answer that supports any use for sync utility existing today, though I must say it hardly seems a very good reason. at this point I'm going with sobrique hypothesis that it's simply a legacy standard of posix, written when Sync was required, and stuck around due to backward comparability, inertia, and just not being worth removing despite it's not really being needed any more. – dsollen Feb 3 '16 at 19:28
3

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.

  • This is all good information, but I'm not certain it answers the question exactly. this describes uses for the system call, but I don't think any of these are uses for the sync utility in bin? Honestly I'm thinking sobrique may be right and it's mostly a legacy of posix standards of the past. – dsollen Feb 3 '16 at 19:24
  • @dsollen Paragraphs 2, 3 and 4 describe uses of the utility. – Gilles Feb 3 '16 at 20:01
  • I call sync for safety before doing a manual forced shutdown: echo b > /proc/sysrq-trigger to avoid the "watchdog did not stop" issue. – xinthose Dec 6 '18 at 22:31
0

Perhaps you know sync is useful when coping some large data to a slow drive. Just to ensure that everything is written to the external drive before pulling it out !

  • I don't think this would help would it? Since sync is both non-blocking and only a suggestion using the command doesn't really do anything more to ensure the data is fully stored before a pull out? – dsollen Feb 3 '16 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.