danjor
Posts: 3
Joined: Tue Apr 23, 2013 4:34 pm

Lost new files after power off

Tue Apr 23, 2013 4:59 pm

Hi!

I have a 512 Mb Raspi B running Rasbian Wheezy.

When I create a file, write something inside and save it, it seems that it is not written immediatly in SD card.
So if I create a file and then power off the raspberry (whithout using the CLI, by simply unplug the power), next time I power on the raspberry I see that modifications wasn't saved, the file is not there.

How can I do to write immediatly in SD card ?

Thanks !

User avatar
rpdom
Posts: 14985
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Lost new files after power off

Tue Apr 23, 2013 6:11 pm

1. Don't pull the power without doing a proper shutdown first. Either from GUI or from command line. Recently written data is held in memory before it is written to the card because it makes the system quite a lot faster.

2. You can force all files to be written to the card by opening a terminal and typing "sync". This instructs Linux to write out all buffered files immediately. This will reduce the likelyhood unwritten files, but is not always 100% effective as the card has its own management system for writing data in order to reduce wear and tear.

3. Always (wherever possible) do a proper shutdown. Wait for the System Halted message. Wait a few more seconds in case the card is doing its own things. Only then should you pull the power.

danjor
Posts: 3
Joined: Tue Apr 23, 2013 4:34 pm

Re: Lost new files after power off

Tue Apr 23, 2013 6:39 pm

Thanks you for your reply!

In fact I'm not able to access to the CLI or GUI. I want to make my raspberry pi autonomous.
The raspberry is powered by a battery, scripts are launched when the OS starts and they do some job inside a loop.

When I want the scripts to stop I pull the power. This is not proper of course.
Sometime it seems that it destroys the script (file size is 0byte).

Files are continously created and modified. As I'm not able to access to GUI and CLI, I'll look for some kind of shutdown switch to safely turn off the Pi. I think this might be a start of solution : http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/

User avatar
symonmj
Posts: 8
Joined: Sun Feb 10, 2013 1:24 pm
Location: South East England

Re: Lost new files after power off

Tue Apr 23, 2013 6:51 pm

Perhaps you could attach a simple push to make switch to a GPIO input and have your program scan for a press in your loop. If a press is detected, you could programmatically intstruct the pi to shutdown cleanly.

import RPi.GPIO as GPIO
from os import system

GPIO.setup([portnumber],GPIO.IN)

# shutdown pi
if GPIO.input([portnumber])
system ("sync")
system ("sudo halt")

...then pull the plug

Just a thought.

MS

User avatar
rpdom
Posts: 14985
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Lost new files after power off

Tue Apr 23, 2013 6:54 pm

Ok, a proper shutdown switch is your best bet, but otherwise you could always put a "sync" in your script after every file update. That will reduce the probability of damage..

danjor
Posts: 3
Joined: Tue Apr 23, 2013 4:34 pm

Re: Lost new files after power off

Tue Apr 23, 2013 7:01 pm

Thank you for your help, I'll try to properly shutdown the Pi by setting a switch and I'll try the sync command. ;)

Return to “Raspbian”