PaxToYou
Posts: 3
Joined: Wed Oct 29, 2014 9:46 pm

How to modify the behaviour of the "halt" command

Mon Jan 19, 2015 10:17 pm

Hi all!

I would like to modify the behaviour of the command "halt" which is been created to shutdown the raspberry system in a safe way.

The natural behaviour of the "halt" command is:
- to dismount everything (action A) and than
- program the raspberry hw to do not use anymore the power (action B).

I would like to add a new "action C" between the actions A and B.

I immagine that after the action A many of the linux functionalities aren't anymore available. So I suppose that the new action C should be coded in "C" at kernel level.

The "action C" I want to realize is very simple: I want to drive a GPIO pin at 1 level and than wait some time (about 1 sec) in a strict loop.
In this way I will be able to switch off the rechargeable battery that supplies the raspberry pi board. So I prevent the dammages caused by an over discharge.

Unfortunately I'm not even able to find the code currently executed by the command "halt".
Does anyone can help me?
Many thanks in advance!
Bye

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: How to modify the behaviour of the "halt" command

Mon Jan 19, 2015 10:54 pm

I'm trying to design something similar for a standalone 24/7 Pi -- if the power fails, shutdown before the UPS fails, then turn off the power. At the same time, have a proper hardware watchdog. If the GPIO pin stops changing, set another pin to shut down the Pi (possibly futile). Switch off the power, pause, and restart. Along the way, provide hardware to put all outputs in a safe state. In my case, I found it quite tricky to work through all the possible sequences of events, and ensure the correct polarity of signals relative to the power-off state. I'll find out sometime if I'm close to correct....

I think your "Switch off" delay will need to be implemented in hardware -- possibly what you are planning. In that case it can be part of the application shutdown, before calling "Halt". Action C will make Action B unnecessary -- there will be no power to the Pi.

What will then switch it back on? A manual switch?
Signature retired

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

Re: How to modify the behaviour of the "halt" command

Tue Jan 20, 2015 6:54 am

During system shutdown the init system will run the scripts in /etc/rc0.d These scripts are actually stored in /etc/init.d and links (shortcuts) are made to certain /etc/rcN.d directories.

Look at some of the scripts in /etc/init.d. You will probably want yours to run just before "halt", but after everything else.

Copying the "umountroot" script to something new ("battery"?), then changing it to do what you want, then running "sudo update-rc.d battery defaults" will enable it. Also, change the Default-Stop value to 0 instead of "0 6". 6 Is reboot and you don't want to turn power off during reboot.

If doing it this way you should put a short delay on your battery so it holds the power for a few seconds to allow the final part of the shutdown to run.

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: How to modify the behaviour of the "halt" command

Tue Jan 20, 2015 4:58 pm

Nothing I got with my RPi kit said anything about 'halt', and I have been using 'shutdown -h now' as I do on my other systems. Is there any difference?

fruitoftheloom
Posts: 23337
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: How to modify the behaviour of the "halt" command

Tue Jan 20, 2015 5:02 pm

experix wrote:Nothing I got with my RPi kit said anything about 'halt', and I have been using 'shutdown -h now' as I do on my other systems. Is there any difference?
Not really http://linux.die.net/man/8/halt
Rather than negativity think outside the box !
RPi 4B 4GB (SSD Boot)..
Asus ChromeBox 3 Celeron is my other computer...

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

Re: How to modify the behaviour of the "halt" command

Tue Jan 20, 2015 5:06 pm

experix wrote:Nothing I got with my RPi kit said anything about 'halt', and I have been using 'shutdown -h now' as I do on my other systems. Is there any difference?
Less to type. :lol:
In Wheezy the "halt" command and "shutdown -h now" are the same thing. Both will shutdown and put your Pi into the low-power halted state.

Both of these commands, and "init 0", will run all the scripts in /etc/rc0.d.

Return to “General discussion”