0

I have a kiosk app written in python which is launched on startup (starting x server but not the window manager as I learnt about here), usbmount is installed to handle automatically mounting pen drives.

I want the application to be update-able by having a pen drive inserted on startup; this works, if the software folder is there it copies over the folder with a temp name, renames what is currently installed (and running) as a backup, renames the temp-named-copy and automatically restarts (executing '/usr/bin/sudo /sbin/shutdown -r now').

The whole software update works with no issues.

If the software isn't on the pen it can update a media folder or an ini configuration file, the process is essentially the same (copy with temp name, backup existing by renaming, rename temp-named-copy) but it doesn't restart. Once updating is complete it continues running the app reading these new files and this seems to work. I've output file sizes during this and they look good, I've used filezilla to pull the files from the pi and they look good, the app runs correctly with the new files.

But frequently (although not 100% reliably) when the raspberry pi/application is restarted without the usb pen drive (the pi is just switched off at the power, there is no shutdown command or procedure, the pen drive can be removed before or after it is switched off) the files that have been copied are empty; the files exist but have 0 size and no content.

I based the initial updater on this which uses shutil.copyfile but doesn't seem to do anything differently, trying through the GUI (using the LXDE winow manager) doesn't have the issue. The only difference between the software update (that works) and the resources update (that sometimes doesn't) is the restart which I would like to avoid in this case (complexity of determining if the resource update should happen or not).

As the files appear to copy but aren't there on restart I wondered if they exist in a cache but haven't been written to disk yet but there doesn't seem to be an shutil version of fsync to force that.

Any thoughts appreciated!

  • 1
    Linux will not flush files directly to disk. When ever I use USB storage I always run "sync" before removing the drive or shutting down. Because even after 10s of minutes the files can still be in memory. – rob Apr 28 '16 at 7:57
  • Thanks rob, can you give me an indication of (having used shutil.filecopy) I force a sync? I can't find anything. – Paddy Apr 28 '16 at 8:26
  • Actually I think you're clearly saying to use the linux sync command: tutorialspoint.com/unix_commands/sync.htm – Paddy Apr 28 '16 at 8:31
0

Thanks to the comment from @rob I got the answer: to use the sync command. This will 'synchronize data on disk with memory'. Seems to work (although slow) and can be invoked in python with:

os.sync()

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.