v17 Script when kodi shutdown
#1
Hi there,

I have a bash script I would like to run as soon as Kodi is shutdown. Is there any way to manage that ?

I'm running a device without keyboard so basically the script is just a bash that start and allow to use button connected to the GPIO to launch program.

Thanks guys
Reply
#2
For Libre- or OpenELEC there is the shutdown.sh file where you are also able to execute additional scripts.

See:

https://wiki.libreelec.tv/index.php?title=Autostart.sh

the same will work OpenELEC. So depending on your shutdown option (let´s assume it´s "poweroff") you have to enter the commands which should be executed before the Pi shuts down.

Example:

Code:
case "$1" in
  halt)
    # your commands here
    ;;
  poweroff)
    ./path/to/script/script.sh
    ;;
  reboot)
    # your commands here
    ;;
  *)
    # your commands here
    ;;
esac

that will execute the script "script.sh" if you "poweroff" the Pi.

Not sure if that will work for OSs like raspbian.
Reply
#3
Hey thanks for answering.

Do I have to create this file or it should be there ?

EDIT :

Well I created the file here : /home/pi/.config/shutdown.sh

I wrote this in :

Code:
#!/bin/bash

case "$1" in
  halt)
    #
    ;;
  poweroff)
   exec /home/pi/loading.sh
    ;;
  reboot)
    exec /home/pi/loading.sh
    ;;
  *)
    # your commands here
    ;;
esac

Doesn't work :/
Reply
#4
If you would have read the wiki I linked, you could have answered your question yourself: Wink

If you are on LibreELEC or OpenELEC you have to create the file under: /storage/.config

If you are not on one of those OSs, this won´t work.
Reply
#5
There we go... Sorry man didn't see that
I was wondering... There was no storage what so ever ^^

Anything I could do then to launch a .sh when shutdown Kodi ?
Reply
#6
What about to mention the OS you are using? As this would help most for the first steps.
Reply
#7
Sorry sure.
I'm running Raspbian, with kodi and emulationstation installed manually.
I put the pi in a psp shell and the buttons are attached to the GPIO

Basically when I boot I would like a script I wrote to start (couldn't manage the script to start after the boot actually. The script works well tho'). Once the script starts you just press a button on the PSP and the software start.
I'd like that when exiting the soft, the script start again.

Example :

I start my psp. The script start. I launch Kodi by pressing a button. I exit Kodi the script start again. I press on a other button, emulationstation is starting. I exit emulationstation the script starts again etc...
Reply
#8
start script on boot:

https://raspberrypi.stackexchange.com/qu...n-start-up

the first answer mentions 2 option to run a scipt on startup. Choose which one will fit best for you. Both will work.

For the shutdown and start the script again...what does exactly happen if you press the button which starts kodi? Does it trigger some script/command to run kodi? Could you show that please?
Reply
#9
This is the script I'm talking about :

Code:
#!/bin/bash
clear
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e "---------------------------------"
echo -e "- Press TRIANGLE ▲ - Kodi"
echo -e "- Press SQUARE   ■ - EmulationStation"
echo -e "- Press CIRCLE   \033[1mO\033[0m - StartX"
echo -e
echo -e "- Press Any Key - Back to shell"
echo -e "---------------------------------"
echo -e

read -s -N 1 answer

if [ "$answer" == $'\e' ]; then
    echo "Kodi is Starting !"
    kodi
elif [ "$answer" = $'a' ]; then
    echo "EmulationStation is Starting"
    emulationstation
elif [ "$answer" == $'\x7f' ]; then
    echo "StartX is Starting"
    startx

else
    clear
    echo "Back to Shell !"
    exit

fi

So if I press Esc on the keyboard or the Triangle button on the PSP it starts Kodi. That's it Smile. Now when I exit Kodi how couyld I possibly run the script above again ?
Reply
#10
I checked your link... and I have a problem : Warning - its important you test your script first and make sure it doesn't need a user to provide a response, press "y" or similar, because you may find it hangs the raspberry pi on boot waiting for a user (who's not there) to do something!
So this is why it's not working. Because my script is based on answer from the user.... What now ? :'(

EDIT : Crontab ?
Reply
#11
Find the solution I edited this file : sudo nano /etc/profile and added my script at the end. After each login it is launching the script. As in raspbian there is the auto login at boot. The script starts Wink

Now just have to find out how to start the script again when I exit Kodi


EDIT :

Found a solution. It's probably not the best one but I changed the code to this :

Code:
#!/bin/bash
clear
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e "---------------------------------"
echo -e "- Press TRIANGLE ▲ - Kodi"
echo -e "- Press SQUARE   ■ - EmulationStation"
echo -e "- Press CIRCLE   \033[1mO\033[0m - StartX"
echo -e
echo -e "- Press Any Key - Back to shell"
echo -e "---------------------------------"
echo -e

read -s -N 1 answer

if [ "$answer" == $'\e' ]; then
    echo "Kodi is Starting !"
    kodi

bash /home/pi/loading.sh

elif [ "$answer" = $'a' ]; then
    echo "EmulationStation is Starting"
    emulationstation

bash /home/pi/loading.sh

elif [ "$answer" == $'\x7f' ]; then
    echo "StartX is Starting"
    startx

else
    clear
    echo "Back to Shell !"
    exit

fi


So after the soft has started the script starts as well in the background. Better than nothing Wink
Reply
#12
I thought about some functions and don't let the script exit after boot.

I'm just on my mobile atm so I'll provide something later in the evening
Reply
#13
First and I know it's nitpicking Wink...don't name a bash script with *.sh as an extension. "filename.sh" will assume your are writing a "sh-script", which is different from "bash-scripts". Just don't use an extension for bash scripts at all. Just name it "filename" ... nothing else.

Example using functions:

explanation....functions are codeblocks inside scripts which can be called from everywhere inside the script....again and again!!

Code:
#!/bin/bash
clear
echo -e "Welcome\x1b[31m Asso'\x1b[0m : $(date)"
echo -e "---------------------------------"
echo -e "- Press TRIANGLE ▲ - Kodi"
echo -e "- Press SQUARE   ■ - EmulationStation"
echo -e "- Press CIRCLE   \033[1mO\033[0m - StartX"
echo -e
echo -e "- Press Any Key - Back to shell"
echo -e "---------------------------------"
echo -e

input() {
  read -s -N 1 answer
  case "$answer" in
    'button_for_triangle')
      echo "Kodi is starting"
      start_kodi
      ;;
    'button_for_rectangle/square')
      echo "EmulationStation is Starting"
      start_emulatiostation
      ;;
    'button_for_circle')
      start_x
      ;;
    *)
      echo "Wrong input. Please try again"
      input
      ;;
    esac
}

start_kodi() {
  kodi
  input
}

start_emulationstation() {
  emulationstation
  input
}

start_x() {
  startx
}

input

This could be an example how the script could work with functions. The script starts at the top, echoing all what you wrote at the top. Then it will read all the functions, but doing nothing with them as you didn't call them yet. Then it will read the command "input" at the bottom and then the input-function will be called and the magic happens Wink.
It will read your input an In "case" you will press the button for kodi, it will call the "start_kodi"-function and kodi will run. After you exit kodi (the script will still run as it waits for the termination of Kodi to go further) it will call the input-function again and you are in your "case" again.
The same will take place for emulationstation. Press a specific key for that application and after exiting it, the input-function will be called and you are in the "case" again and the script waits for a specific input.

You could understand all the above as a big loop which will run as long as you exit it

if the case won't work those
Code:
$'\e'
then you are probably able to map another key for that buttonpress. Probably some real charakter such as "k" for Kodi and "e" for Emulationstation. Then the case part for the input function could look as:

Code:
input() {
  read -s -N 1 answer
  case "$answer" in
    'k')
      echo "Kodi is starting"
      start_kodi
      ;;
    'e')
      echo "EmulationStation is Starting"
      start_emulatiostation
      ;;
    'o')
      start_x
      ;;
    *)
      echo "Wrong input. Please try again"
      input
      ;;
    esac
}

I couldn't runtime test this script and maybe it's a bit hacky Wink. But maybe it gives you another idea how things might be done. So you don't need to care how to call the script multiple times as that would cause multiple subprocesses probably which I would try to avoid if I were you Wink.

That script hasn't the option to exit. Maybe you are able to map "x" to the "X"-Button for exit and add:

Code:
'x')
      exit 0
      ;;

before the "*)" part at the case-statement.
Hope it helps.

Cheers

Edit:

Btw....don't use just "exit" to exit a script. Use "exit 0" instead. That will mean that the script run successfully. If you will ever write other scripts for your systems, you might need different exit-statuses and explain (inside the script with comments) what the specific exit-status could mean. Assume some command didn't run or you check for something which might fail (probably if a folder exists), then you should exit the script with anything else beside "0" (zero). for example "exit 1", or "exit 2" and so on. Everything else beside "0" will mean that the script exits with an issue or something didn't worked as expected or how it should.
See that for example: https://github.com/LibreELEC/LibreELEC.t...ts/getedid

That's a script I wrote some time ago where I have explained all the exit-statuses at top.
Reply
#14
Hey there !

That was very well explain thank you very much for that. I will work on it again, because you are right, multiple process is probably not the best.

Thanks again I will try that.
Reply



Logout Mark Read Team Forum Stats Members Help
Script when kodi shutdown0
This forum uses Lukasz Tkacz MyBB addons.