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: 36337
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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.
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.

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: 36337
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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.
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.

afletch
Posts: 30
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 ([email protected])
#
# 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.)

Return to “Beginners”