1

I have few .txt files I use to store configuration information for some python and shell scripts on my Pi. They are occasionally read from and very rarely written to. The Pi runs 24/7 mostly but will occasionally be unplugged from power so I can move it to a new location. I have noticed that sometimes the .txt files would suddenly become empty. Any idea what causes this? How it can be prevented?

2
  • 1
    What does unplug mean? Unplug from power, ethernet, wifi, USB storage ...? Or from all to move the RasPi? How do you unplug it from power? Never do it before a graceful shutdown by software.
    – Ingo
    May 16 '20 at 18:18
  • 1
    @Ingo the Pi is enclosed securely and running an automated script. I am unable to securely shutdown from SW. Any suggestions?
    – presish
    May 18 '20 at 18:14
1

You may want to set up auto-save since I see that you mention "very rarely written to". I'm not sure what the bug is, but I suggest that you may want to save the file more frequently and set a backup (or a copy of the file) in case there are errors again.

2
  • 1
    So you suggest creating a copy of the file and save the original to the copy frequently? and load the copy to the original every reboot/system startup?
    – presish
    May 15 '20 at 21:02
  • I think so. Also just a "backup" solution in case there is data corruption. May 15 '20 at 21:04
3

It sounds like some files that are open/being read during shutdown may become corrupted and wiped. An easy solution is to include a button on the enclosure you are using that registers an interrupt and can issue a shutdown command to the RPi. This link describes how to implement an interrupt using one button and a few lines of python. I would also use the os library and the classic sudo reboot now or sudo shutdown command.

As also suggested, adding a cron job that backs up the files periodically isn't a bad idea. An @reboot ... line can also be added to your crontab to check if the file is empty and recover the last backup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.