ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Run a python script when RPi is told to reboot/halt

Fri Apr 27, 2018 4:56 pm

Hi

I want to run a simple python3 file (sends an instruction via serial UART) before the reboot/halt of the pi.

I've tried messing with init.d rc6 and rc0 like some suggestions on the web but really nothing worked for me.
I have the feeling that python may be "disconnected" before the actual script is supposed to run, and so nothing happens.

The information on this topic is usually not very clear and i think it's old or doens't work in debian.

Does any one knows a link were this is well explained for the Pi and that is recent.
Or explain yourself How should I do this

Thanks

User avatar
DougieLawson
Posts: 41316
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Run a python script when RPi is told to reboot/halt

Fri Apr 27, 2018 5:56 pm

Try creating sudo nano /etc/systemd/system/rcshut.service

Code: Select all

[Unit]
Description=/etc/rc.shutdown
Before=shutdown.target

[Service]
ExecStart=/bin/true
ExecStop=/etc/rc.shutdown
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Enable that with sudo systemctl enable rcshut.service
Start it for this boot with sudo systemctl start rcshut.service

That should run /etc/rc.shutdown at any halt, shutdown and reboot.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: Run a python script when RPi is told to reboot/halt

Fri Apr 27, 2018 6:55 pm

Thanks i will try it, but where do i put the python file or the bash script?

User avatar
DougieLawson
Posts: 41316
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Run a python script when RPi is told to reboot/halt

Fri Apr 27, 2018 8:13 pm

In /etc/rc.shutdown (copy /etc/rc.local as an example) or change the Execstop line.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

afletch
Posts: 34
Joined: Tue Nov 13, 2018 3:17 pm

Re: Run a python script when RPi is told to reboot/halt

Thu Sep 26, 2019 4:24 am

I've followed the above steps and cannot get my "shutdown script" to run at shutdown.

My rc.shutdown

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Turn off LED lights-lights out!
sudo python /home/pi/NeoSectional/kill.py &


exit 0
This points to kill.py

Code: Select all

#!/usr/bin/env python3
# NeoPixel library strandtest example
# Author: Tony DiCola (tony@tonydicola.com)
#
# Direct port of the Arduino NeoPixel library strandtest example.  Showcases
# various animations on a strip of NeoPixels.

import time
from neopixel import *
import argparse

# LED strip configuration:
LED_COUNT      = 50      # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN        = 10      # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 10      # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL    = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53

# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()

print('true!')
for i in range(strip.numPixels()):
        strip.setPixelColor(i, Color(0, 0, 0))
strip.show()

(I'm trying to "turn off" my string of LEDs at shutdown.)

PieterG
Posts: 33
Joined: Thu Nov 10, 2016 4:41 pm

Re: Run a python script when RPi is told to reboot/halt

Thu Apr 09, 2020 10:54 am

I am trying to do the same thing.

In my .service script I have

Code: Select all

[Unit]
Description=show halt message ín display
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
RemainAfterExit=yes
User=root
Group=root
ExecStart=/bin/true
ExecStop=python3 /home/pi/halt.py &

[Install]
WantedBy=multi-user.target
and yes, it is enabled.

But checking the shutdown.target, reboot.target and halt.target, these seem disabled. Now I am a linux noob in a way, but it looks they are disabled by default in Raspbian. (and Debian when I look at another system....)
multi-user.target is enabled by the way.
Can this have anything to do with it?
Or is it python is already dead before this runs?


[update]
Yes, so I am a noob.. double...or better did not get the error in my python script... when I crawled through my log all of a sudden it bit me... I had a loop going on in "halt.py"... stupid me, hit me, kick me..

Anyway, it works as expected... still I am a noob. :oops:



Tx.

Return to “Beginners”