Moode Forum
How to properly add add python script to autostart - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: Community (https://moodeaudio.org/forum/forumdisplay.php?fid=18)
+--- Forum: General Discussion (https://moodeaudio.org/forum/forumdisplay.php?fid=21)
+--- Thread: How to properly add add python script to autostart (/showthread.php?tid=7031)



How to properly add add python script to autostart - vol.rom - 10-24-2024

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-picase/blob/master/SafeShutdown.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


RE: How to properly add add python script to autostart - Tim Curtis - 10-24-2024

Try posting in their repo
https://github.com/RetroFlag/retroflag-picase/issues


RE: How to properly add add python script to autostart - vol.rom - 10-24-2024

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.


RE: How to properly add add python script to autostart - vol.rom - 10-24-2024

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