In /boot/config.txt I did put
dtoverlay=gpio-shutdown,active_low=1,gpio_pull=up
Now if I short GPIO 3 - the Pi does shutdown immediately.
But how can I run some shell script before shutting down via GPIO 3 shorting?
In /boot/config.txt I did put
dtoverlay=gpio-shutdown,active_low=1,gpio_pull=up
Now if I short GPIO 3 - the Pi does shutdown immediately.
But how can I run some shell script before shutting down via GPIO 3 shorting?
 
    
    Try adding a systemd service (example) which runs your script shortly before shutdown:
[Unit]
Description=Shutdown actions
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/path/to/script
[Install]
WantedBy=sysinit.target
ExecStart action is set to a program which finishes right away, so you need RemainAfterExit=true to keep the service running from the systemd point of view. Don't forget to enable your service with sudo systemctl enable my.service
If you need to distinguish between shutdown and reboot, one possible solution is to check which target is active from inside your script. Try running systemctl is-active poweroff.target and systemctl is-active reboot.target, and analyze its return code (0=active, 1=inactive) or output (it outputs "active"/"intactive" as a string).
As an alternative, try the following service (it should run only on reboot, not on a power-off):
[Unit]
Description=Shutdown actions
DefaultDependencies=no
Before=reboot.target
Conflicts=reboot.target
The rest is the same as above
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/path/to/script
[Install]
WantedBy=sysinit.target
 
    
    ExecStop=/bin/bash -c 'systemctl list-jobs | grep -Eq 'reboot.target.*start' || sudo /home/admin/bin/scripts/script.sh'
        – VextoR
                Apr 25 '23 at 19:38