Unix folklore: using multiple sync commands

December 28, 2005

There is persistent bit of folklore in the Unix world that you should do several sync commands before shutting down or rebooting a Unix machine. Even today, you'll see a lot of people following this folklore, generally using 'sync; sync; sync'.

People who do this irritate me, because they are doing it wrong and in a way that makes the whole folklore ineffectual.

The sync folklore dates from the very old days when Unix was primitive and two things were true. First, the sync() system call that the sync program uses just queued dirty buffers to be flushed and didn't guarantee that they had been writing to disk. Second, the kernel itself didn't force all buffers to be written to disk as part of the shutdown or reboot process.

In this sort of old Unix environment, you needed to sync by hand before rebooting the system to get all the dirty buffers to disk. But just one sync wouldn't necessarily do it; what you needed to do was run sync and then wait for a bit. But for some reason this was never made an explicit part of the instructions; instead people were told 'do several sync commands, typing each by hand'. This should take enough time that the buffer flush started by the first sync would have completed by the time you ran reboot (especially if you were using a slow hardcopy console terminal, which was common then).

So the right command sequence of folklore is:

# sync
# sync
# sync
# reboot

(Without typeahead, of course.)

But this mutated to just 'sync three times', so of course people started writing 'sync; sync; sync'. And then the whole mess descended into folklore and superstition. (Sometimes you see people advising that you should always run two or three syncs instead of one, even if you're not shutting down the system.)

These days it is an atrociously bad Unix system that doesn't flush all buffers on shutdown by default, so the whole 'sync before reboot' folklore is unnecessary. Often sync(2) is also synchronous (and thus the sync command too), although the other behavior is still permitted; see the Single Unix Standard sync(2) manpage.

Written on 28 December 2005.
« A thought about free software licenses
What I use asserts for »

Page tools: View Source, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Dec 28 00:52:13 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.