Years ago in UNIX class, I seem to remember there was a program that would cause all the data blocks scheduled for output to be actually written to the drive. This was meant to be run just before shutting down the computer, so that data would not be lost. What program is that?
It is usually called sync
, but it should get run automatically during the shutdown sequence, unless you are just flipping off the power?
I was a Unix sysadmin back in the day, dealing mostly with Berkeley Unix. IIRC, there was some known technical glitch with that, resulting in a rule-of-thumb that one should always run sync(8) twice before beginning the shutdown process. (And even then, as DPRK mentions, running shutdown(8) will still run it automatically as well.)
I have no recollection why that was.
Thanks. I’m afraid I’d gotten into the bad habit of just turning the computer off.
Modern computers have a soft poe
were switch that sends a shutdown signal to the computer. The computer will finish all writes to disk before shutting off. Just don’t cut the power or flip the switch on the back.
It’s only (some) computers made before 1998 or so that you need to worry about properly shutting down before hitting the power button.
Maybe it’s just this computer. It’s old (although not that old) and has lots of problems. I really need to replace it, but … procrastination.
My desk top computer’s power off switch does not turn power off. It just turns the power on if the computer is off. To turn the computer off I run a program that allows me to shut down the computer. It handles all disk writes and clean killing of processes. No accidental shutting off the computer when it’s done this way.
Once in a while I’ve needed to pull the plug. These have always been hardware malfunctions. Mainly overheated video card. I’ve since replaced it and everything runs smooth now.
I have a recurring problem with my laptop. The Bluetooth connection turns off and nothing I can do turns it back on. Except the emergency shutdown (press and hold the on button for 5-10 seconds) that prevents the computer from saving the Bluetooth state. An ordinary shutdown doesn’t work, only the emergency one does.
This is Windows, not Unix, BTW. I was following the suggestion from my son who worked on the NT project that Windows is built on.
@tavaritz , is your power switch a rocker-type switch or a push-button? On modern computers, you can use it to turn power off by pressing and holding it for a while, as @Hari_Seldon noted just above. I believe if you shut down this way, it will send a shut-down signal to the processor, allowing it to flush all disk data and park the heads, providing that the system is at least running enough to do just that.
I think the issue was that the sync command didn’t actually perform the sync. It queued the request and then exited. So you could outrun it. Running it twice probably just gave the system time to catch up with your fingers.
You could also shut the system down and explicitly not run sync. If you suspected a failing disk this was a good idea. These were very different times.
Aha. I’m vaguely (very vaguely) remembering this, now that you mention. So if I wrote a shell script to run two syncs in a row and then run shutdown, it might outrun the sync requests?
I’m wondering if a second consecutive sync request (the attempt to queue the request, not the actual sync) would block until the first one was done?
Unix systems will usually link a soft power switch to code that generates a signal that is delivered to the init process. Different signals cause different actions. Systemd is another matter.
I think every sysadmin on the planet has mistyped
kill -1
At least once, and left the - off.
The system drops to single user mode as the kill delivers a SIGTERM to process number one, aka init.
Yeah, same question came to me. Not sure. It may well be different on different Unix variants. Once we got journaling file systems it all changed anyway.
My desk top computer’s power switch is push button. Haven’t seen computers with rocker-type switches in ages. And furthermore I’ve disabled the power off function from my computer’s off switch in BIOS, so that no-one (including me) can accidentaly turn my computer off.
Fun, related fact: If you have a spinning disk hard drive, it has a cache of some size that holds pending writes. This makes the drive way more efficient because the drive firmware can accept writes to cache, then route an efficient path for the head on the disk to get it on disk without the OS having to wait for the second (slow) part.
So what happens to the data in the cache when the power goes away? The answer is that the drive uses the spinning disk as a flywheel to power the rest of the drive for long enough to emergency write all the cached writes to disk.
I believe that SSDs also have a cache and use some little capacitors to run long enough to write it when power goes away, but I’m not sure about that.
I would like to have a cite for this. I don’t buy it. First you would have to have the motor for the platters to also be a generator. That generator would have to supply enough power to move the read/write heads back and forth at some really unbelievable speeds. And to top it all off, you would also have to have some device that can detect the speed of the platters, and allows the controller to adjust the speed of the writing to match the speed of the platters as they slow down.
I would be cool, but not really worth the expense for most hard drives.
I should have been more clear: not all HDDs do this, but some really do.
Here’s a Datacenter WD drive that does.
Looks like I was wrong about it writing to disk. It writes to other nonvolatile storage. I had assumed that it had a special sector of the disk reserved for this near where the drive head was parked, so it wouldn’t have to move them all around, just move them to that one spot and coast (sort of) to park, but flash makes more sense.
In the 1990s when I was writing a filesystem and needed intimate knowledge of how drives worked (and before there was Flash in drives) many (perhaps most) drives did use the rotational momentum of the platters to power the electronics briefly after a power failure. But the goal was simply to complete any write in progress. That is, it would allow successfully finishing writing one single sector, to avoid producing a corrupted sector. There wasn’t enough time to actually move the heads and write other sectors.