• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to shutdown/reboot the cleanest possible way

PacketMan

Aspiring Daemon

Thanks: 132
Messages: 855

#1
This, from a different discussion, reminded me that I wanted to ask a question ages ago, but never got around to asking.
Try issuing a couple of sync(8) commands before umounting it.
A long time ago I did a basic UNIX course, on System V I think it was, and the instructor said "do a sync before you shutdown, this will ensure disk cache is written out".

So, I could do, and usually always do a shutdown -r now. Or I could do a halt -r. Or I could do a reboot -r.

But there seems to be a subtle difference in the 'language' of the man pages. halt(8), and reboot(8) state "The halt and reboot utilities flush the file system cache to disk." shutdown(8) however does not say that. It does also say that you can use shutdown -i to cause shutdown to use halt, instead of init.(8) Why would I want to shutdown to to use halt instead of init, if, it indeed does do cache / file system flushing already? This suggests to me that shutdown does not ensure/do disk cache writing before shutdown. Is this true, or am I missing a key something?

So my question is, if I want to shutdown or reboot the system in the cleanest possible way, what command including flags, would achieve this?. Maybe I should be using shutdown -o -r now?? Or should I always do a sync(8) first?
 

herrbischoff

Active Member

Thanks: 69
Messages: 165

#2
I would be very surprised if the FreeBSD developers overlooked a clean shutdown/reboot that is executed by the default commands. It would be highly confusing to me if there existed an "even cleaner" way to do it. If there was, it would surely be implemented already.
 

gkontos

Daemon

Thanks: 455
Messages: 2,094

#3
In older days we used to issue 2 sync commands before we halt a system. I remember Solaris in the nineties. That's not necessary anymore :)
 

SirDice

Administrator
Staff member
Administrator
Moderator

Thanks: 6,125
Messages: 27,000

#4
In older days we used to issue 2 sync commands before we halt a system.
That's how I know the command exists.

It should not be needed any more. A proper shutdown -p now (shutdown/turn off) or shutdown -r now (shutdown and restart) already takes care of this. But in the thread I suggested using it there seem to be some problems with a filesystem not being clean after a shutdown. That why I suggested trying to force the sync(8) to make sure everything is written and closed properly. I'm basically grasping at straws.
 

PacketMan

Aspiring Daemon

Thanks: 132
Messages: 855

#5
I would be very surprised if the FreeBSD developers overlooked a clean shutdown/reboot that is executed by the default commands.
I didn't say they overlooked anything. Often well thought out design will result in 'ways' that might seem odd or lacking to others. Its possible in the grand scheme of things the developers purposely made differing commands that do differing things. Perhaps shutdown(8) does not actually do a couple items, like disk cache writing, with the expectation that the user knows what he/she is doing. That surely is an expectation the in UNIX and FreeBSD worlds, that the user well understands what the command does. Why have three ways to shutdown a machine if they are all identical. Why not collapse them down into one command, with the various flag options? Maybe we can, but time and resources are spent on more important things.

It should not be needed any more. ....That why I suggested trying to force the sync(8) to make sure everything is written and closed properly. I'm basically grasping at straws.
Yeah knew that, but this had reminded me to seek the clarification I meant to do ages ago. A lot has changed since then.

I'd really like to know what difference there is in shutdown -p now versus shutdown -o -p now.
 

phoenix

Administrator
Staff member
Administrator
Moderator

Thanks: 1,129
Messages: 3,949

#6
halt(8) and reboot(8) short-circuit the shutdown process a bit by not going through all of the RC process for stopping processes properly before killing the OS.

shutdown(8) does a proper, ordered shutdown of all processes (via RC), waits a bit for any slow ones, then does a forced kill of anything still running, spends up to 3-ish minutes waiting for disk buffers to be flushed to the disk, then issues ACPI commands to either halt the system (-h) but leave power running to the motherboard), power-off completely (-p), or reboot (-r).
 

Chris_H

Aspiring Daemon

Thanks: 136
Messages: 951

#7
The best way to guarantee everything is flushed to disk, is via halt(8). While it is a bit brutal. It's still the most effective way to guarantee all buffers are flushed to disk, and the least likely to leave you with unwanted "disk not properly (un|dis)mounted" related messages, and other disk related issues.
In short; brutal, but effective. :)

--Chris
 

PacketMan

Aspiring Daemon

Thanks: 132
Messages: 855

#8
So how about shutdown -o -p now? Best of both worlds? I'll have to fart around with it someday and see what subtle or not-so subtle differences there are.
 

poorandunlucky

Well-Known Member

Thanks: 26
Messages: 359

#9
I thought all shutdown methods simply ran the same shutdown init script and that the only difference was in the ACPI command...
 

Rin Cat

New Member


Messages: 11

#10
just know shutdown is not same as halt and reboot. :eek:
I think they all do sync during the processing when I was reading the output.
 

Eric A. Borisch

Well-Known Member

Thanks: 204
Messages: 313

#11
Based on my reading of the source...

All of the various methods call reboot(2) which eventually calls kern_reboot() which in turn calls bufshutdown() (the source of the "Syncing disks, buffers remaining..." messages.) There are sync()s sprinkled in shutdown/init/halt, and in reboot(), too, so unless you pass flags asking to disable sync (don't do that) sync(2) will be called multiple times in the process.

Explicitly:
  • shutdown -r now signals init, which tears down services nicely (with real waits / cleanup as needed for each server process specified by rc.d/* files) and then calls reboot(). This is all bounded by kern.init_shutdown_timeout (default = 2 minutes)
  • halt/reboot kills (SIGTERM) everything and then has a heuristic waiting to see if there are no page-ins for three seconds to determine everything is happy (up to 1 minute) and then kills (SIGKILL) anything remaining and finally calls reboot().
So, while all (unless called with the don't-sync flags) will give everyone a chance clean up and go home, shutdown (without the -o flag) will actually run the shutdown processes defined in rc.d/*, including any service specific cleanup (removal of temp files, etc.) that may not occur when the processes are TERM-ed. If something is keeping a process from nicely shutting down for a while, AND it doesn't cause any page-ins for three seconds, halt/reboot may KILL a still-closing process; shutdown gives it more time, and can have (depending on the rc scripts) actual waits for exits rather than the heuristic wait-for-"idle".

There is an interesting '-N' mode for reboot(8) that sounds like it could have some corner-case utility, but I would stick with 'shutdown -r now' unless you know you're stuck in a corner. ;)
 
Top