[SOLVED] Kodi on RPi4 on LibreELEC - run custom script upon "Kodi power-off"

  • Hello forum,


    I would like to run a custom BASH script as the user chooses to power-down Kodi via the GUI.


    The script uses variable assignment, the date fuction, netcat and a while/do loop.


    I know cron cannot be used for running scripts at/before shutdown.


    I'm running Kodi 19.3 Matrix on a 4GB RPi with LibreELEC 10.0.1.


    Thanks for your time.


    Best regards,

    Rembrandt


    PS: EDIT 1 this does not work - tested

    Would I be able to start a BASH script via cron @reboot that checks ps aux | grep kodi

    for the likes of :

    1067 root 0:00 {kodi.sh} /bin/sh /usr/lib/kodi/kodi.sh --standalone -fs

    1081 root 7:59 /usr/lib/kodi/kodi.bin --standalone -fs

    and if these, or one of them disappear, assume that Kodi is going down?

    Thanks.

    Edited 3 times, last by Rembrandt: New thoughts (January 15, 2022 at 7:45 PM).

  • cause "ps aux | grep kodi" returns 2 values and that's a "beloved" mistake ?

    without seeing your script it's a wild guess what doesn't work ...

    anyway:

    pgrep kodi.bin ?

    or

    pgrep kodi.sh

    parameter:

    pgrep --help

    Edited once, last by GDPR-7 (January 15, 2022 at 9:01 PM).

  • Thanks JoeAverage,

    The code that runs in cron @reboot is:

    Code
    while [ $( ps aux | grep -v grep | grep -c kodi ) -eq 2 ] ; do sleep 1; done; /storage/bin/RunScriptAtKodiPowerDown.sh

    I could use pgrep instead, had forgotten about it.

    However, I think that the order of shutting down services/deamons/processess etc prevents this from ever exiting the while-loop, i.e. the above BASH code gets 'unloaded', removed from memory before the while condition ever reaches "not-equal". This is a hunch.

    Perhaps there is another way to do this?

    BR

    Rembrandt


    PS: EDIT 1

    I suspect running / starting my script at power-down needs to be organised differently, so that the RPi does not keep shutting down parts of the OS etc as my BASH script is still running, i.e. my script would need to be run between the signal to power down and the start of the actual power-down procedure.

    Edited once, last by Rembrandt (January 16, 2022 at 9:42 AM).

  • OK forum, thank you so far.

    To recap, I'm trying to run a custom script as Kodi "Power Off System" is clicked on a RPi-4 under LibreELEC.

    I now have the following code.

    /storage/.config/shutdown.sh which has execute rights :

    Code
    -rwxr-xr-x    1 root     root           474 Jan 16 10:40 /storage/.config/shutdown.sh

    shutdown.sh :

    The "power-off" case does not seem to get triggered and I do not want it in all-other cases.

    When I run the above ( power-off case) code in a script, saved as a sh file, directly from the command line, it works each and every time.

    That was from Raspbian - I have to correct some things - I apologise.

    Code corrected.

    Do I perhaps need to enable the use of the shutdown.sh ?

    Perhaps I have to add the full paths to each command in the shutdown.sh script ?

    BR

    Rembandt

    Edited 5 times, last by Rembrandt (January 16, 2022 at 12:36 PM).

  • Yup, duh. Thanks frakkin64,

    Upon Kodi power-off-system, I want to send a message to a headless RPi4 to shutdown properly via "shutdown -P now" or similar.

    Would there be another way that avoids having to use the GPIO to communicate between the two RPis?

    Thanks

  • Upon Kodi power-off-system, I want to send a message to a headless RPi4 to shutdown properly via "shutdown -P now" or similar.


    Would there be another way that avoids having to use the GPIO to communicate between the two RPis?

    I wouldn't turn them off, but that's me. The power draw is so minimal, it's honestly not worth the hassle, wear & tear.

    Other than GPIO, which you could use the gpio-poweroff overlay on the one controlling the shutdown and gpio-shutdown overlay on the one receiving it. There does appear to be some downsides with that approach, as in you need to remove power externally.

    Another option is could be simply pinging from the headless RPi4 and initiating a shutdown? Or wait and see what others have to say, now they understand your end goal here.

  • The linked post (number 9) contains the solution at

    External Content pastebin.com
    Content embedded from external sources will not be displayed without your consent.
    Through the activation of external content, you agree that personal data may be transferred to third party platforms. We have provided more information on this in our privacy policy.

    Looks like that will install a new service, and will trigger on any shutdown event (poweroff/halt/reboot). So something to consider with what is trying to be achieved.

    This option would be nice to see as an enhancement to kodi-autostart.service passing a start/stop parameter to autostart.sh, but I guess the problem there is no way of knowing if the script is written for an argument.

  • Another solution could be to use irexec and map a script to some IR remote control key. The configuration is very simple and it works great.

    ghtester
    January 13, 2021 at 6:20 PM
  • mglae's post #12's link to a solution worked for me; I just changed "exec /bin/sh" to "exec /bin/bash" but followed the other instructions to the letter. Thanks!

    frakkin64's ping idea: duh, again, I use it to find computers that come online, so I could have used it for computers going offline/powering down too.

    ghtester, thanks, I had considered a 433MHz transmitter in the Pi and power sockets as a potential follow-up to this, but that's another subject for another time.

    Thank you all!

    Edited once, last by Rembrandt (January 16, 2022 at 3:03 PM).