Hi
Issues with using a momentary switch to turn off the Rpi.
In my app code, the user can press a web button to turn off the Rpi. The result is that 25 seconds is waited before the Rpi shuts down.
This 25 second delay is set in option [2] Power cut delay after shutdown.
This allows my service to close gracefully.
Now, if I attach a switch to the GPIO4 pin I can allow my user to turn off the Rpi via a press of the button. Most of the time this works. But sometimes, the red light on the Witty board stays on indicating that not everything has shutdown. I then must disconnect the USB power bank and the LIPO battery and then reconnect all to start the Rpi up again.
This is because the 25 second delay is ignored. I have timed it manually and it seems to shutdown with 4-5 seconds.
I thought if I put a delay in the beforeShutdown.sh:
logger "start shutdown! #write to syslog file sudo service MyService stop #enfoce my service to stop logger "Hello there!" #write to syslog file sleep 20 logger "Oops! I fell asleep for 20 seconds!" #write to syslog file echo "done"
But looking in the syslog everything seems to be ignored.
Is this by design? If so how can this ever work?
Thanks
I managed a workaround by implementing a sleep function within my own app after trapping the shutdown event. But, I would of thought the delay to shutdown would be honoured within your firmware? I was going to add a delay at that level but it would be not a good paradigm to keep adding it after every update you did.
Every command you put in the beforeShutdown.sh file should contain its full path (as indicated in the comments in the file). That script is run as root user, so it doesn't know the actual path of the commands.
Fail to do so will make the commands being ignored, because they were not found during the boot.
Hi
But none of tjos commands need a full path. I have run this stand alone by ./beforeShutdown.sh and works fine.
The issue is when the GPIO4 is pulled down this script is not evoked.
Hi
But none of tjos commands need a full path. I have run this stand alone by ./beforeShutdown.sh and works fine.
The issue is when the GPIO4 is pulled down this script is not evoked.
By running a script standalone you probably mean you run it as the user which is logged in, usually pi. It has a different 'environment'. You cannot draw any conclusions from running as pi.
I have been using these scripts for a few months now and they do work. I'll show how I use them below. One 'exception' I found was that beforeShutdown.sh is not executed when you issue the shutdown command in a terminal.
Perhaps by studying these, you can see how things can be made to work.
#!/bin/bash # file: beforeShutdown.sh # ar - 13-11-2024 # This script will be executed after Witty Pi receives shutdown command (GPIO-4 gets pulled down). # If you want to run your commands before turnning of your Raspberry Pi, you can place them here. # Raspberry Pi will not shutdown until all commands here are executed. # # Remarks: please use absolute path of the command, or it can not be found (by root user). # Remarks: you may append '&' at the end of command to avoid blocking the main daemon.sh. # datetime=$(date +'[%Y-%m-%d %H:%M:%S]') /usr/bin/echo $datetime ">>> Executing beforeShutdown.sh" # beforeShutdown.sh will apparently not be run in connection with a terminal shutdown command. ##/usr/bin/echo $datetime ">>> Copy system time to RTC" ##source /home/pi/wittypi/utilities.sh; system_to_rtc ##/usr/bin/echo $datetime ">>> Stopping some services before the shutdown command" ##/usr/bin/echo $datetime ">>> issued by the WittyPi daemon in order to shorten the shutdown." ##/usr/bin/systemctl stop unbound ##/usr/bin/systemctl stop pihole-FTL ##/usr/bin/systemctl stop chrony
Ignore the comments at the end, they are just a leftover from earlier attempts to achieve something. The only current effect is a line in the wittypi.log showing the script ran.
#!/bin/bash # Last modified by ar -- 30-10-2024 # file: afterStartup.sh # # This script will run after Raspberry Pi boots up and has finished running the schedule script. # If you want to run your commands after boot, you can place them here. # # Remarks: please use absolute path of a command, lest it can not be found (by root user). # Remarks: you may append '&' at the end of a command to avoid blocking the main daemon.sh. # datetime=$(date +'[%Y-%m-%d %H:%M:%S]') /usr/bin/echo $datetime ">>> Executing afterStartup.sh" /usr/bin/echo $datetime ">>> Re-enable chrony service after start-up" /usr/bin/echo $datetime ">>> as Witty Pi disables NTP service ..." # Enable & start chrony; no other documented mechanism # to prefer chrony over systemd-timedated actually worked ... sudo systemctl enable --now chrony /usr/bin/sleep 10 # Loop until system clock has been synchronized, then copy time to RTC. /home/pi/wittypi/systemTime2RTC.sh &
I want to run chrony and it needs to be re-enabled after every restart.
#!/bin/bash # Last modified by ar -- 30-10-2024, 03-11-2024 datetime=$(date +'[%Y-%m-%d %H:%M:%S]') /usr/bin/echo $datetime ">>> Wait until system clock is synchronized ..." until [ $(timedatectl show --property=NTPSynchronized --value) ] do /usr/bin/echo $datetime ">>> Waiting ..." /usr/bin/sleep 10 done ##/usr/bin/echo $datetime ">>> Copy system time to RTC." source /home/pi/wittypi/utilities.sh; system_to_rtc > /dev/null
This script is of my own making ...
<2024-12-09 02:31:05> Done :-) [2024-12-09 22:51:27] Shutting down system because button is clicked or GPIO-4 is pulled down. [2024-12-09 22:51:27] >>> Executing beforeShutdown.sh [2024-12-09 22:51:27] Halting all processes and then shutdown Raspberry Pi... [xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.21) is started. [xxxx-xx-xx xx:xx:xx] System: Debian GNU/Linux 12 (bookworm), Kernel: Linux 6.6.62+rpt-rpi-v8, Architecture: arm64 [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 4 Model B Rev 1.5 [xxxx-xx-xx xx:xx:xx] RTC offset register has value 0x75 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [2024-12-09 22:52:07] Done :-) [2024-12-09 22:52:07] Firmware ID: 0x37 [2024-12-09 22:52:07] Firmware Revision: 0x07 [2024-12-09 22:52:07] Current Vout=5.18V, Iout=0.68A [2024-12-09 22:52:07] System starts up because the button is clicked. [2024-12-09 22:52:12] >>> Executing afterStartup.sh [2024-12-09 22:52:12] >>> Re-enable chrony service after start-up [2024-12-09 22:52:12] >>> as Witty Pi disables NTP service ... Synchronizing state of chrony.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable chrony [2024-12-09 22:52:14] File "schedule.wpi" not found, skip running schedule script. Created symlink /etc/systemd/system/chronyd.service â /lib/systemd/system/chrony.service. Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service â /lib/systemd/system/chrony.service. [2024-12-09 22:52:36] Send out the SYS_UP signal via GPIO-17 pin. [2024-12-09 22:52:36] >>> Wait until system clock is synchronized ... [2024-12-09 22:52:36] Writing system time to RTC... <2024-12-09 22:52:36> Done :-) [2024-12-09 22:52:36] Pending for incoming shutdown command... [xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.21) is started. [xxxx-xx-xx xx:xx:xx] System: Debian GNU/Linux 12 (bookworm), Kernel: Linux 6.6.62+rpt-rpi-v8, Architecture: arm64 [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 4 Model B Rev 1.5 [xxxx-xx-xx xx:xx:xx] RTC offset register has value 0x75 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [2024-12-10 02:30:35] Done :-) [2024-12-10 02:30:35] Firmware ID: 0x37 [2024-12-10 02:30:35] Firmware Revision: 0x07 [2024-12-10 02:30:35] Current Vout=5.18V, Iout=0.86A [2024-12-10 02:30:35] System starts up because it previously reboot. [2024-12-10 02:30:40] >>> Executing afterStartup.sh [2024-12-10 02:30:40] >>> Re-enable chrony service after start-up [2024-12-10 02:30:40] >>> as Witty Pi disables NTP service ... Synchronizing state of chrony.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable chrony [2024-12-10 02:30:41] File "schedule.wpi" not found, skip running schedule script. Created symlink /etc/systemd/system/chronyd.service â /lib/systemd/system/chrony.service. Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service â /lib/systemd/system/chrony.service. [2024-12-10 02:31:06] Send out the SYS_UP signal via GPIO-17 pin. [2024-12-10 02:31:06] >>> Wait until system clock is synchronized ... [2024-12-10 02:31:06] Writing system time to RTC... <2024-12-10 02:31:07> Done :-) [2024-12-10 02:31:07] Pending for incoming shutdown command... [xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.21) is started. [xxxx-xx-xx xx:xx:xx] System: Debian GNU/Linux 12 (bookworm), Kernel: Linux 6.6.62+rpt-rpi-v8, Architecture: arm64 [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 4 Model B Rev 1.5 [xxxx-xx-xx xx:xx:xx] RTC offset register has value 0x75 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [2024-12-11 02:30:30] Done :-) [2024-12-11 02:30:30] Firmware ID: 0x37 [2024-12-11 02:30:30] Firmware Revision: 0x07 [2024-12-11 02:30:30] Current Vout=5.18V, Iout=0.79A [2024-12-11 02:30:30] System starts up because it previously reboot. [2024-12-11 02:30:35] >>> Executing afterStartup.sh [2024-12-11 02:30:35] >>> Re-enable chrony service after start-up [2024-12-11 02:30:35] >>> as Witty Pi disables NTP service ... Synchronizing state of chrony.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable chrony [2024-12-11 02:30:36] File "schedule.wpi" not found, skip running schedule script. Created symlink /etc/systemd/system/chronyd.service â /lib/systemd/system/chrony.service. Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service â /lib/systemd/system/chrony.service. [2024-12-11 02:31:02] Send out the SYS_UP signal via GPIO-17 pin. [2024-12-11 02:31:02] >>> Wait until system clock is synchronized ... [2024-12-11 02:31:03] Pending for incoming shutdown command... [2024-12-11 02:31:03] Writing system time to RTC... <2024-12-11 02:31:03> Done :-) [xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.21) is started. [xxxx-xx-xx xx:xx:xx] System: Debian GNU/Linux 12 (bookworm), Kernel: Linux 6.6.62+rpt-rpi-v8, Architecture: arm64 [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 4 Model B Rev 1.5 [xxxx-xx-xx xx:xx:xx] RTC offset register has value 0x75 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [2024-12-12 02:30:41] Done :-) [2024-12-12 02:30:41] Firmware ID: 0x37 [2024-12-12 02:30:41] Firmware Revision: 0x07 [2024-12-12 02:30:41] Current Vout=5.25V, Iout=0.66A [2024-12-12 02:30:41] System starts up because it previously reboot. [2024-12-12 02:30:46] >>> Executing afterStartup.sh [2024-12-12 02:30:46] >>> Re-enable chrony service after start-up [2024-12-12 02:30:46] >>> as Witty Pi disables NTP service ... [2024-12-12 02:30:47] File "schedule.wpi" not found, skip running schedule script. Synchronizing state of chrony.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable chrony Created symlink /etc/systemd/system/chronyd.service â /lib/systemd/system/chrony.service. Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service â /lib/systemd/system/chrony.service. [2024-12-12 02:31:14] Send out the SYS_UP signal via GPIO-17 pin. [2024-12-12 02:31:14] >>> Wait until system clock is synchronized ... [2024-12-12 02:31:15] Writing system time to RTC... [2024-12-12 02:31:15] Pending for incoming shutdown command... <2024-12-12 02:31:15> Done :-) [xxxx-xx-xx xx:xx:xx] Witty Pi daemon (v4.21) is started. [xxxx-xx-xx xx:xx:xx] System: Debian GNU/Linux 12 (bookworm), Kernel: Linux 6.6.62+rpt-rpi-v8, Architecture: arm64 [xxxx-xx-xx xx:xx:xx] Running on Raspberry Pi 4 Model B Rev 1.5 [xxxx-xx-xx xx:xx:xx] RTC offset register has value 0x75 [xxxx-xx-xx xx:xx:xx] Seems RTC has good time, write RTC time into system [xxxx-xx-xx xx:xx:xx] Writing RTC time to system... [2024-12-13 02:30:33] Done :-) [2024-12-13 02:30:33] Firmware ID: 0x37 [2024-12-13 02:30:33] Firmware Revision: 0x07 [2024-12-13 02:30:33] Current Vout=5.11V, Iout=0.9A [2024-12-13 02:30:33] System starts up because it previously reboot. [2024-12-13 02:30:38] >>> Executing afterStartup.sh [2024-12-13 02:30:38] >>> Re-enable chrony service after start-up [2024-12-13 02:30:38] >>> as Witty Pi disables NTP service ... Synchronizing state of chrony.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable chrony [2024-12-13 02:30:39] File "schedule.wpi" not found, skip running schedule script. Created symlink /etc/systemd/system/chronyd.service â /lib/systemd/system/chrony.service. Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service â /lib/systemd/system/chrony.service. [2024-12-13 02:31:04] Send out the SYS_UP signal via GPIO-17 pin. [2024-12-13 02:31:04] >>> Wait until system clock is synchronized ... [2024-12-13 02:31:05] Pending for incoming shutdown command... [2024-12-13 02:31:05] Writing system time to RTC... <2024-12-13 02:31:05> Done :-)
Some lines in the log are added by the scripts shown above, others are added by other wittypi code. You will have to check for yourself, which lines are written by the modifies before and after scripts. There are some shutdowns and restarts initiated by pressing the button and others by a reboot command in crontab. Especially in the latter part of this log file (I posted the tail only).
The reason I use chrony is that this Pi is providing time services to my LAN (DD-WRT router and all Raspberry Pi clients and my Linux workstation, if present; smartphones do what they want ...). Wittypi disables chrony, so I need to re-enable and start it after every reboot. RPi system time is copied to the RTC as soon as the system time has been synchronized with an NTP server. This is just to make sure that they are in sync.
This setup has been working fine for a month or more in two locations (two LANs, two PiHole/time servers).
It is just an example of working with the before and after scripts.
Thanks. I never got an alert to say you replied so sorry I have only just noticed now. I am using root as the usr login.But I wil look through how yu have done it.
many thanks