MattiMTB
Posts: 7
Joined: Fri Jun 28, 2019 3:14 pm

Auto Shutdown of Pi after closing a GUI Python Script

Thu Sep 12, 2019 12:30 pm

Hello fellow Pi people,

My project is soooooo close to being ready for implementation, but there is one thing stopping me...... getting the Pi to shutdown immediately after the script is closed.

In a nutshell, my project is a sports timer for a dog sport called flyball. After months of work I have successfully ran my script with a prototype model...... the laser tripwires are working thanks to the use of an Op-Amp and some IR Phototransistors.

I've used the code from the GPIOZERO documentation to create a poweroff procedure:-

Code: Select all

from subprocess import check_call
--------
lots of code
--------
--------
class InputEnd():

	def finish():
		global enabled
		enabled = False
		leds.close()
		gobutton.close()
		frontpole.close()
                crosspole.close()
		tk.destroy()													
		check_call(['sudo', 'poweroff'])
If I run the script either from the lxterminal or from Geany, it works fine..... the pi will switch off when I close my script.
I have the script automatically running after boot up now (that was a mission and a half to get working), but when I close the script it goes back to the PIXEL desktop and doesn't shut down.
Any advice I can get to resolve this would be much appreciated.

Some additional information:-
Because my script uses a GUI written with Tkinter, I needed the script to load after the Pi had booted in Desktop mode.

I got the autorun to work first by creating a launch.sh script which contains this:-

Code: Select all

#!/bin/bash
cd FlyballTimer
sudo python3 FlyballTimerPi.py

Permissions were changed to make the launch.sh executable, then I modified the lxsession autostart file:-

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Code: Select all

@lxpanel –profile LXDE-pi
@pcmanfm –desktop –profile LXDE-pi
/home/pi/launch.sh
@xscreensaver -no-splash
point-rpi

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

Re: Auto Shutdown of Pi after closing a GUI Python Script

Thu Sep 12, 2019 12:58 pm

In python3 you can use

Code: Select all

import os
cmd="sudo shutdown -h"
os.system(cmd)
That will terminate your python3 program as part of the shutdown process so once the shutdown command is in there's nothing else you can do.

Shutdown needs sudo permissions so you may need to add a line to /etc/sudoers if you've changed it from the default (very open and very insecure Raspian settings).
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.

MattiMTB
Posts: 7
Joined: Fri Jun 28, 2019 3:14 pm

Re: Auto Shutdown of Pi after closing a GUI Python Script

Sat Sep 14, 2019 1:08 am

Thank you Dougie.

I'll try this out as soon as I get a chance. Working overtime and I've got a geek session on this afternoon at Jaycar Electronics learning about the other board..... Arduino.

I came across a tutorial from a business called Core Electronics which shows you how to set up a physical shut down button by using code which runs in the background of the Pi's workings. This may be a compromise option I'll need to take, but I don't know yet.

I'll let you know how it all works out. Thanks again for your reply. :D

MattiMTB
Posts: 7
Joined: Fri Jun 28, 2019 3:14 pm

Re: Auto Shutdown of Pi after closing a GUI Python Script

Sun Sep 15, 2019 3:08 am

Hello again Dougie,

I tried out your method, but I get the same result. IF I manually start up my script then close it, it will automatically shut down.
IF the script is started automatically on boot up, it will return to the desktop window when the script is closed.

Looking closer at the script, I noticed it doesn't 'actually' close as such. I have threads running in my script and they appear to be preventing the script from closing properly.
I think I will need to examine ways to close the threads on termination of the script. I think that if I set the threads as daemon=True, this will cause the threads to be killed on termination of the script.

When I get the chance I will have a go at it.

Return to “Python”