I was working on my Pi and accidentally disconnected it from power. I have read that disconnecting power while the SD card is being written to can cause SD card corruption. How often does that really happen?
Rarely, but still happens. Besides, there are a lot of factors to consider to guess the chance of SD card corruption including but not limited to:
- How busy the card was when power got disconnected
- What the operation was before power was disconnected (read or write)
- How often a "dirty" shutdown is done
- Electrostatic discharge (ESD)
- Whether the SD card is mounted as read-only or writable
- SD card quality
- Bad luck or someone wishing bad luck upon you. Pointed out by SlySven
- Software quality (whether what the software does causes corruption or not)
- Power noise
- Voltage to the SD card
- Clock stability to the SD card
- Processor and friends' clock speed (GPU, SDRAM, etc.)
- Overclocks are known to cause corruption
- Software configuration
- File system contingency plans (how the file system handles corruption)
- OS' contingency plans (how the OS handles data corruption)
- Data redundancy
- SD card temperature (you should not put a lighter or stove under an SD card)
- How much stress the SD card is having (physical stress, bending, etc.)
- SD card connection quality
- SD card connector quality
- Normal wear and tear
- Magnetic fields
- Most SD cards are immune to this
- Cosmic rays
- Environmental radioactivity
- An SD card is more likely to get corrupted inside a nuclear reactor
- Acts of God
- War (regular or nuclear)
- Apocalyptic scenario
- Rogue computer virus destroying everything
- Corrosion from normal wear and tear
My point is that SD card corruption is hard to guess, but using common sense will save you the hassle of recovering corrupted data.
Shut down using the command
sudo shutdown -h now and wait for the green LED to blink. That's the "clean" way of shutting down.
I think PandaLion98's answer is fairly comprehensive so this is more of an anecdotal observation based on:
- Having owned at least one pi for more than three years.
- Having had one pi (first a B, then a B+) on 24/7 for most of that 3 years.
- Having a few other pi's (another B, and a 2B) that I normally use headless.
In the last category, I'm regularly doing things that may leave a pi in an unresponsive, or at least network disconnected state, and the way I have networking configured once it is down I would have to plug a screen and keyboard in to get it back up again. This is pretty easy since I have them at hand but to be honest: I don't bother unless I am curious about investigating something that happened, which usually I'm not because the information relevant to me is logged anyway.
So I just pull the plug. The number of times that happens is anywhere between several times a day and several times a month. I have no qualms or worries about this because never once in all that time, on any of the pis have I seen any sign of SD card corruption having occurred. There's also occasional power outages here, power bars get accidently turned off or unplugged, etc. There's also one I use with a camera and battery pack outside and I sometimes just let it keep going until it runs of of juice.
That said, there are a few caveats to this anecdote:
Generally the pis are not very busy WRT to SD card I/O. I'm not running them as Tor nodes, etc. This is significant because if the power is cut while a lot of I/O is going on, there is more likely to be corruption. However, corruption of that sort, based on my experience with other linux machines over the past decade or so, is almost always fixable by
fsckat boot, which unless you've screwed with the configuration will happen in such cases. I have a couple of times seen a (spinning disk) drive left in an unusable state, and in both cases the root problem turned out to be hardware failure. Spinning disks do not last for ever (and neither do SD cards; if you want some of my thoughts on that see here).
I tend to use a power supply (2-2.5 amps) that is ample for what I am doing -- wifi dongle, sometimes RF keyboard/mouse combo, maybe an external HD (only works without a hub on the B+ and 2B), maybe a small servo. However, occasionally I've used them with smaller ones, down to 1 and I think even 0.75 A.
I don't spend a lot of money on SD cards. Not because I have anything against brand names (I have at least one Sandisk one) but because I've gotten them together in kits that included a power supply and case and those are unbranded. They do usually have class 10 stamped on them. Also there's a near-by electronics store that sometimes has unbranded micro-SDs in a jar on sale at the cash register. Finally, I don't think it matters much on the pi, the controller itself is too slow and the machine itself is just not high performance.
Note that all this doesn't mean I have a pile of them and are switching them in and out all the time; I don't -- once I have one in the state I want it usually stays in the pi for months, I'm sure there's one that's been there for more than a year running 24/7.
There seems to be an IMO highly unusual degree of paranoia about SD cards amongst pi users. I say that because our use of them is statistically irrelevant -- I like many other people also use them in phones, tablets, and cameras, and have never had an issue there either. What's more, if there were a real problem, that statistical use is so high it would probably make TV network news. I'd think photographers are the ones with the best experience regarding how long an SD card can last and what difference the various qualities of cards really amounts to.
Anyway, I'd put that paranoia down to two causes:
There's this issue that may have affected some people for some period of time, I don't know, but it is in the past now.
People don't rant online about how their equipment is working just fine. An absolutely horrible form of reasoning is to claim that "there must be problem because I've seen hundreds of complaints about this issue online". First, there have been millions (as in 2-4+ by now, I think) of pis sold. Second, a lot of those complaints (which I think realistically would be better measured in the dozens) are obviously by the same irate/upset/frustrated person posting multiple places, or having someone else re-post it for whatever reason. Third, from observing things here over the past 2-3 years, a lot of problems come down to ignorance and incompetence. There is nothing shameful about incompetence, I am incompetent on a unicycle, however, I am sensible enough not to blame it when I fall down.