up vote 5 down vote favorite
1
share [g+] share [fb]

I still see people recommend use of "sync; sync; sync; sleep 30; halt" incantations when talking about shutting down or rebooting Linux.

I've been running Linux since its inception and although this was the recommended procedure in the BSD 4.2/4.3 and SunOS 4 days, I can't recall that I had to do that for at least the last ten years, during which I probably went through shutdown/reboot of Linux maybe thousands of times.

I suspect that this is an anachronism since the days that the kernel couldn't unmount and sync the root filesystem and other critical filesystems required even during single-user mode (e.g. /tmp), and therefore it was necessary to tell it explicitly to flush as much data as it can to disk.

These days, without finding the relevant code in the kernel source yet (digging through http://lxr.linux.no and google), I suspect that the kernel is smart enough to cleanly unmount even the root filesystem and the filesystem is smart enough to effectively do a sync(2) before unmounting itself during a normal "shutdown"/"reboot"/"poweorff".

The "sync; sync; sync" is only necessary in extreme cases where the filesystem won't unmount cleanly (e.g. physical disk failure) or the system is in a state that only forcing a direct reboot(8) will get it out of its freeze (e.g. the load is too high to let it schedule the shutdown command).

I also never do the "sync" procedure before unmounting removable devices, and never hit a problem.

Another example - Xen allows the DomU to be sent a "shutdown" command from the Dom0, this is considered a "clean shutdown" without anyone having to login and type the magical "sync; sync; sync" first.

Am I right or was I lucky for a few thousands of system shutdowns?

link|improve this question

50% accept rate
feedback

3 Answers

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

link|improve this answer
Thanks for the explanation. Just to clarify what you are saying - "the developers have ensured that halt does the right thing" means that "halt" calls "sync" too or that it runs the proper init scripts which eventually call "sync"? What about already being at single-user mode and just calling "halt"? Am I correct in my assumption that the Linux kernel is just smart enough not to really shut down abruptly but will unmount all file systems before shutting down? – Amos Shapira Feb 21 '10 at 11:16
1  
halt calls shutdown which calls umount which performs a sync. – DaveG Feb 21 '10 at 19:24
Thanks DaveG. So what you are saying is that it all happens in user-level and the kernel won't umount and sync filesystems on its own? In any case, it sounds like the "sync; shutdown" ceremony is redundant today. – Amos Shapira Feb 22 '10 at 1:15
feedback

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.

link|improve this answer
feedback

What about remounting a readonly file system as read/write ? I have mounted rootfs as readonly and I remount it as read/write with this command : mount -o remount,rw / and then when I have changed rootfs I run mount -o remount,ro / but I see some problems when I check fs with fsck.

Does second command call SYNC before mounting as readonly ?

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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