0

I am working on a project which needs relay to be controlled using raspberry pi. I have set up my script to run on boot up but when power is turned off sometimes the relay is left in ON state as the program exits mid way which is not desired. Is it possible to terminate an auto run script safely once power is switched off?

And is there a way to safely shut down pi on power off as the abrupt power off may corrupt my SD card or even damage my pi.

I have googled a bit regarding this and came across this article http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi which needs some extra circuitry.

Is there an alternative method which does not needs an extra circuit?

2
  • I'm afraid Rpi doesn't have a brown out detector as far as I know. Commented Jul 1, 2015 at 11:47
  • "Is there an alternative method which does not needs an extra circuit?" -- No. The pi is a regular computer in this sense. You can either shut it down properly, or you can just pull the plug. In the latter case, there is no means by which the operating system can predict what is going to happen, and when it happens there's no more anything as electronics require electricity to work.
    – goldilocks
    Commented Jul 1, 2015 at 13:29

1 Answer 1

1

It depends.

You might want to invest in special power supply like that power supply but it is expensive. It is the only certain way to save your device from power outages long enough for them to shutdown. It could be cheaper to buy something like that battery power supply, and try to switch to it when power dies down. Some circuit with transistor blocking power consumption from batteries while normal power supply is supported. You would need then breadboard power supply to power your PI and switch between power supplies. You could also connect some GPIO pin to alternate power supply to detect when it switched and initialize safe shutdown.

But you also said "no extra circuit". Only way to do that would be to add startup script to restart whatever services need to be restarted on startup. Just check if they have properly shut down and if not - shut them down before you start them again.

3
  • There are also various people who make and sell power off switches for the pi that will trigger the system to shut down before they actually kill the power -- this probably takes 3-5 seconds. Search online for "raspberry pi power off switch".
    – goldilocks
    Commented Jul 1, 2015 at 13:27
  • Sorry, I think I focused too much on 'Abrupt' keyword :P Commented Jul 1, 2015 at 13:38
  • I don't disagree with you, this is a decent answer -- I was just adding that, although I think that Ayesha is already aware of what I am referring to anyway.
    – goldilocks
    Commented Jul 1, 2015 at 13:44

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