DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Shutdown at command level

Sun Sep 09, 2018 6:42 am

Hi,

I am wondering if something i do is state-of-the art, although the app works perfectly.

Setup: a Rpi Zero W is designed to be a numeric keyboard witth a 3x4 matrix input keyboard.
Power comes from an USB connection and any data captured, is collected by a desktop program (via SSH)

On the Rpi runs a Python3 data capture program that also validates, etc...
This program runs continuously, UNTIL a "secret code" is typed on the matrix keyboard.

Then the python program executes the following statement, shutting down the Rpi completely.
os.system('sudo shutdown -h now')
Question:
This is kinda "brutal" . Should i not terminate the python program properly first? How can it terminate it's own execution?
But then i still need to somehow issue the shutdown statement (?).

Any thoughts on this ? Some equivalent of the old ".BAT' files in DOS : first terminate the program, then shut down the Rpi?

Yes , i could issue the commands from the desktop, but i would prefer to contain it in the Rpi.

thx,
Paul

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

Re: Shutdown at command level

Sun Sep 09, 2018 7:31 am

The python program will be sent signals during the shutdown. The first is a "please close down now", if that doesn't work it gets sent a "end now!" request. If it still hasn't ended (which it should have done on the first request), it will just be terminated. There should be no need to do anything special, but you could possibly trap the first signal if you need to do some specific clean up.

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: Shutdown at command level

Sun Sep 09, 2018 7:55 am

Hi,

thx, i realize that messages will be sent if i do it this way. But you say that does not matter.

After some searching i found 50% of another solution:

I could end my "While true : " loop with a "break" statement if the "secret code" is entered.
That would allow for a "finally:" with some cleanup statement.

But now i still have to shutdown the rpi altogether.
If i put that command as last "finally:" statement , it's a little cleaner, but it still may cause messages to be sent.

I feel that as long that the shutdown command is issued from the python program, it's not state of the art.
Or shouldn't i bother?

Paul

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Shutdown at command level

Sun Sep 09, 2018 9:11 am

Programs on Linux have default signal handlers. The Python interpreter is a program too !

Of course, Python being a full-fledged programming language allows you to override
the default signal handlers inside your own program itself.

If you want to it "correctly", overriding the SIGTERM handler of your very own program would be the way to do it.

The shutdown command is actually rather gentle. It tends to trigger SIGTERM handlers in all running programs sooner or later, which in itself should result in a orderly and clean shutdown.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

User avatar
DougieLawson
Posts: 37127
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shutdown at command level

Sun Sep 09, 2018 9:26 am

DPaul wrote:
Sun Sep 09, 2018 7:55 am
I feel that as long that the shutdown command is issued from the python program, it's not state of the art.
Or shouldn't i bother?
It's not difficult to add a SIGTERM handler.

https://stackoverflow.com/a/31464349

Test that with
python tester.py &
killall -s 15 tester.py
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: Shutdown at command level

Sun Sep 09, 2018 9:54 am

OK, thanks, this is what i found:
---------------------------------------------------
import signal

run = True

def handler_stop_signals(signum, frame):
global run
run = False

signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)

while run:
pass # do stuff including other IO stuff
----------------------------------------------------------------
if i understand this correctly:....

instead of
finally:
GPIO.cleanup()
os.system('sudo shutdown -h')

i should do ...
finally:
GPIO.cleanup()
handler_stop_signals

?
Paul

klricks
Posts: 6793
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Shutdown at command level

Sun Sep 09, 2018 2:43 pm

DPaul wrote:
Sun Sep 09, 2018 6:42 am
...
os.system('sudo shutdown -h now')
Question:
This is kinda "brutal" . Should i not terminate the python program properly first? How can it terminate it's own execution?
But then i still need to somehow issue the shutdown statement (?).
...
If you issue the shutdown -h without the 'now' then the system will delay shutdown for 1 min. This should give the program plenty of time to terminate before shutdown.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

PhilBr
Posts: 65
Joined: Tue Oct 31, 2017 5:28 pm

Re: Shutdown at command level

Sun Sep 09, 2018 6:35 pm

It should all work if you simply close everything down (close files, reset GPIO, etc), issue the shutdown then

Code: Select all

sys.exit(0)
That should terminate the program and be both simple and safe.
Phil

DPaul
Posts: 97
Joined: Fri Oct 13, 2017 7:39 am

Re: Shutdown at command level

Mon Sep 10, 2018 6:27 am

OK, thanks, plenty of possibilities.
My problemis solved !

Paul

Return to “Beginners”