Reputation: 167
Ok so running raspberry pi (RetroPie) and have a python script running a shutdown, but I want to avoid any possible data corruption when the power button is pressed.
Is there a way to check to see if any of the connected drives are being written to and if so to wait and check again every 2 seconds until it detects nothing else is being written and then continues shutting down.
I am using the retroflag-picase -SafeShutdown.py
#!/usr/bin/env python3
from gpiozero import Button, LED
import os
from signal import pause
powerPin = 3
resetPin = 2
ledPin = 14
powerenPin = 4
hold = 1
led = LED(ledPin)
led.on()
power = LED(powerenPin)
power.on()
#functions that handle button events
def when_pressed():
led.blink(.2,.2)
# Do check here #
os.system("sudo killall emulationstation && sleep 5s && sudo shutdown -h now")
def when_released():
led.on()
def reboot():
os.system("sudo killall emulationstation && sleep 5s && sudo reboot")
btn = Button(powerPin, hold_time=hold)
rebootBtn = Button(resetPin)
rebootBtn.when_pressed = reboot
btn.when_pressed = when_pressed
btn.when_released = when_released
pause()
Upvotes: 1
Views: 85
Reputation: 2537
You can use os.sync()
to wait till everything is written to disk. Or you can use the cmd sudo sync
instead of sleep 5s
before reboot
Upvotes: 2