Is execution of sync(8) still required before shutting down linux?

Solution 1:

The reason people would run sync; sync before a halt is because the halt command wouldn't shutdown the system cleanly on older linuxes. The correct way to do this on SYSVr4 systems has always be to tell init to move to a different run level.

BSD and SunOS 4 aren't SYSVr4 operating systems which is why they differ. Solaris (SunOS 5) is SYSVr4 and Linux picks out bits of the SYSVr4 standard that it wants to use.

Using halt is actually a pretty bad way of doing it on most UNIXes (Linux being one of the exceptions) since it doesn't actually run through the init scripts to perform things like stopping processes and unmounting disks - it just stops the processor.

If you can guarantee that you'll never ever, ever use any kind of UNIX system than Linux then you can keep using halt - if there's a chance you're going to use other UNIXes then I'd recommend getting into the habit of using init _runlevel_ or shutdown.

The shutdown command actually tells the init process to change its run level run level - in doing so init then proceeds to run each of the K* init scripts and S* init scripts associated with that run level. One of the scripts in run-level 0 performs the unmounting of the filesystems.

On Linux the halt command just calls the shutdown command unless the run level is already 0 (shutting down) or 6 (rebooting) anyway; so no loss there.

The act of unmounting a filesystem using umount will sync the data to the disk before it unmounts it.

If you've been running sync; sync; halt on Linux you'll have been okay with the filesystem state because the developers have ensured that halt does the right thing; however it would be more correct to use: shutdown now

Solution 2:

I can only speak to why you would issue sync multiple times. The command schedules the flush to disk but returns before the actual flush completes. Any subsequent sync command will block until any outstanding flush is in progress before scheduling another flush and exiting. Therefore, sync; sync ensures a synchronous flush. You do not need to do it more than 2 times, nor bring sleep into the mix.

Solution 3:

The use of multiple sync calls was to allow the OS and disks time to flush the write queues. "sync; sync; sync" wasn't considered that useful; one did "sync<cr> sync<cr> sync<cr" and the delay while your ASR-33 did the carriage return/newline provided enough delay. Halt always did call sync; the question was whether there would be enough time to flush the queues before power was removed.

The original poster's sync; sleep 30 is more in keeping with what was intended.

Solution 4:

Those of you telling us all that "sync;sync;sync" has no purpose are revealing your age.

Back in the good ol' days, before Unix was something for teenagers, we used to have to use TAPE for our streaming/backup needs. Often-times, we'd mount a tape-based filesystem to stream backups to, and so on. This one long thin band of magnetic plastic tape was all some of us had, to store our files on ..

The 'sync;sync;sync' command was a way that these old tape machines could be told to rewind themselves all the way to the end (before shutdown) - they had firmware on board that would receive the sync cmd (like all good filesystems do), and if it was followed almost immediately by two more sync buffer commands, the tape drive itself would interpret this to mean "rewind the tape and unmount it". There wasn't any way to tell the tape drive to rewind, beyond this method, and it sort of stuck .. This habit then passed on into the word when hard drives became more available - us crusty old operators don't just realloc() our muscle memory you know! I believe it achieved folklore status soon after tapes become less common and hard-drives became more available, but it still has its uses for those of us with tape drives.