Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


How to properly add add python script to autostart
#1
Hello I'm using Retroflag NESPi Case for my Raspberry Pi.

I tried to add this script but without success: https://github.com/RetroFlag/retroflag-p...hutdown.py
I deleted os.system("sudo killall emulationstation") and tried to run it but I failed to do so.

Any ideas how to set it properly?

Thanks,
Volodymyr
Reply
#2
Try posting in their repo
https://github.com/RetroFlag/retroflag-picase/issues
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#3
I did post there, but I also wanted to know how to properly install such scripts on Moode OS. Additionally, I have an extra fan installed on the board, and I've written a script that activates the fan when the temperature gets too high.
Reply
#4
Solved by converting py script to bash
Steps:

sudo nano /opt/SafeShutdown.sh

Code:
#!/bin/bash

# GPIO pin definitions (BCM pin numbers)
powerPin=3    # BCM pin 3, physical pin 5
ledPin=14     # BCM pin 14, TXD
resetPin=2    # BCM pin 2, physical pin 13
powerenPin=4  # BCM pin 4, physical pin 5

# Initialize GPIO pins using pigpio
init_gpio() {
    pigs m $powerPin r  # Set powerPin to input (read)
    pigs m $resetPin r  # Set resetPin to input (read)
    pigs m $ledPin w    # Set ledPin to output (write)
    pigs w $ledPin 1    # Turn LED on
    pigs m $powerenPin w  # Set powerenPin to output (write)
    pigs w $powerenPin 1  # Enable power
}

# Wait for the power button to be pressed and hold for 1 second, then power off
poweroff() {
    while true; do
        if [ $(pigs r $powerPin) -eq 0 ]; then
            sleep 1  # Wait for 1 second to confirm button is held down
            if [ $(pigs r $powerPin) -eq 0 ]; then
                sleep 5
                sudo shutdown -r now
            fi
        fi
        sleep 0.1
    done
}

# Blink the LED while the power button is pressed
led_blink() {
    while true; do
        if [ $(pigs r $powerPin) -eq 0 ]; then
            while [ $(pigs r $powerPin) -eq 0 ]; do
                pigs w $ledPin 0  # LED OFF
                sleep 0.2
                pigs w $ledPin 1  # LED ON
                sleep 0.2
            done
        fi
        sleep 0.1
    done
}

# Reset the Raspberry Pi when the reset button is pressed
reset_pi() {
    while true; do
        if [ $(pigs r $resetPin) -eq 0 ]; then
            sleep 5
            sudo shutdown -r now
        fi
        sleep 0.1
    done
}

# Initialize GPIO and start the processes
init_gpio

# Run each function in the background
poweroff &
led_blink &
reset_pi &

# Wait for processes to complete (they never will in this loop)
wait



sudo apt-get update
sudo apt-get install pigpio python3-pigpio
sudo systemctl enable pigpiod
sudo systemctl start pigpiod

chmod +x /opt/SafeShutdown.sh

sudo nano /etc/systemd/system/SafeShutdown.service

Code:
[Unit]
Description=SafeShutdown
After=network.target

[Service]
ExecStart=/opt/SafeShutdown.sh
WorkingDirectory=/opt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target

sudo systemctl enable SafeShutdown.service
sudo systemctl start SafeShutdown.service

sudo reboot - h now

done Smile
Reply


Forum Jump: