# Actually writing data to a disk drive

**URL:** <https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624>\
**Category:** Factual Questions\
**Created:** [October 17, 2021, 2:35am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624 "2021-10-17T02:35:21Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtilque](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dtilque](https://boards.straightdope.com/u/dtilque)\
**Post date:** [October 17, 2021, 2:35am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/1 "2021-10-17T02:35:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DPRK](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DPRK](https://boards.straightdope.com/u/DPRK)\
**Post date:** [October 17, 2021, 2:55am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/2 "2021-10-17T02:55:05Z")

</div>

It is usually called `sync`, but it should get run automatically during the shutdown sequence, unless you are just flipping off the power?

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [October 17, 2021, 3:20am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/3 "2021-10-17T03:20:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dtilque](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dtilque](https://boards.straightdope.com/u/dtilque)\
**Post date:** [October 17, 2021, 5:56am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/4 "2021-10-17T05:56:39Z")

</div>

Thanks. I’m afraid I’d gotten into the bad habit of just turning the computer off.

---

<div class="post-metadata">

**Author:** ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)\
**Post date:** [October 17, 2021, 6:22am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/5 "2021-10-17T06:22:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dtilque](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@dtilque](https://boards.straightdope.com/u/dtilque)\
**Post date:** [October 17, 2021, 6:30am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/6 "2021-10-17T06:30:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tavaritz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tavaritz](https://boards.straightdope.com/u/tavaritz)\
**Post date:** [October 17, 2021, 6:57am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/7 "2021-10-17T06:57:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hari\_Seldon](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/hari_seldon/32/5173_2.png) [@Hari\_Seldon](https://boards.straightdope.com/u/Hari_Seldon)\
**Post date:** [October 17, 2021, 4:52pm UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/8 "2021-10-17T16:52:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [October 18, 2021, 2:39am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/9 "2021-10-18T02:39:51Z")

</div>

> [@tavaritz](#):
>
> My desk top computer’s power off switch does not turn power off. It just turns the power on if the computer is off.

@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.

---

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [October 18, 2021, 3:43am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/10 "2021-10-18T03:43:28Z")

</div>

> [@Senegoid](#):
>
> I have no recollection why that was.

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.

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [October 18, 2021, 3:48am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/11 "2021-10-18T03:48:04Z")

</div>

> [@Francis\_Vaughan](#):
>
> 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.

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?

---

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [October 18, 2021, 3:50am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/12 "2021-10-18T03:50:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Francis\_Vaughan](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/francis_vaughan/32/3093_2.png) [@Francis\_Vaughan](https://boards.straightdope.com/u/Francis_Vaughan)\
**Post date:** [October 18, 2021, 3:54am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/13 "2021-10-18T03:54:11Z")

</div>

> [@Senegoid](#):
>
> 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?

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.

---

<div class="post-metadata">

**Author:** ![tavaritz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tavaritz](https://boards.straightdope.com/u/tavaritz)\
**Post date:** [October 18, 2021, 8:45am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/14 "2021-10-18T08:45:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [October 18, 2021, 9:20pm UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/15 "2021-10-18T21:20:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SoToasty](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sotoasty/32/2803_2.png) [@SoToasty](https://boards.straightdope.com/u/SoToasty)\
**Post date:** [October 22, 2021, 10:28pm UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/16 "2021-10-22T22:28:44Z")

</div>

> [@iamthewalrus\_3](#):
>
> 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 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.

---

<div class="post-metadata">

**Author:** ![iamthewalrus\_3](https://avatars.discourse-cdn.com/v4/letter/i/258eb7/32.png) [@iamthewalrus\_3](https://boards.straightdope.com/u/iamthewalrus_3)\
**Post date:** [October 22, 2021, 11:14pm UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/17 "2021-10-22T23:14:30Z")

</div>

I should have been more clear: not all HDDs do this, but some really do.

[Here’s a Datacenter WD drive that does.](https://drivesolutions.com/western-digital-re-datacenter-5tb-7200rpm-sata-6gb-s-128mb-cache-3-5-enterprise-class-hard-drive-wd5001fsyz.html)

> [@](#):
>
> Power Loss Data Protection (5TB and above) - Upon power loss, the Re drive utilizes stored spindle energy to back up the HDD cache to onboard flash. This allows deeper write queues which boosts performance, while minimizing data loss/corruption such as write splices that can occur during unexpected power losses.

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.

[And some SSDs do use capacitors for the same purpose](https://www.seagate.com/solutions/network-attached-storage/power-loss-data-protection/)

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 23, 2021, 1:26am UTC](https://boards.straightdope.com/t/actually-writing-data-to-a-disk-drive/952624/18 "2021-10-23T01:26:31Z")

</div>

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.
