Script aborted after closing OSMC

Hello.
First, I’ve a Raspberry Pi 2 with OSMC RC.
I’m executing a script inside OSMC, from a custom main menu button action: RunScript(/home/osmc/myscript.py). This script will launch other application, but will close OSMC first, however nothing gets executed after I close OSMC.
My script looks like:

#!/usr/bin/python
import subprocess
subprocess.call('sudo sh /home/osmc/limelight/limelight.sh &', shell=True)

I’ve also tried calling the .sh file with:

subprocess.Popen(['sudo', 'sh', '/home/osmc/limelight/limelight.sh'])

with

os.execvp("sudo", ["sudo"]+["sh"]+["/home/osmc/limelight/limelight.sh"]) 

and

os.system("sudo sh /home/osmc/limelight/limelight.sh")

Every one of them works in a SSH shell or directly in raspberry command line after closing manually osmc
In a quick teste, in a test .sh file:

echo "before close" >> /home/osmc/test.log
sudo systemctl stop mediacenter
echo "after close" >> /home/osmc/test.log

All I have in test.log is “before close” if launched inside osmc with RunScript (btw also tired System.Exec but can’t get that to even call the script).
Any ideia why this is happening?

You might wanna check out @mcobit’s post about using RetroPie with OSMC. His retropie.sh script will close Kodi and start EmulationStation. Once you close EmulationStation, Kodi restarts. Sounds like what you are trying to accomplish.

Also a link to his github page:

The only way I could accomplish that is to detach the closing command from shell with nohup and adding the starting of emulationstation into the custom mediacenter script as the stoppting of the service calls the mediacenter script and cleans up before exiting what closes the current tty apparently and your script will get closed, too.

So I use nohup amd openvt to get it running on a new virtualterminal. Sounds complicated and it is but I havent found another way.

2 Likes

Thanks for the replies, I’ll try it.

Of course, if you find a better solution, let me know.
I would love to have a solution where it is not necessary to modify the mediacenter script as installing your application and the retropie script, with the same solution they would overwrite each other.
Maybe there could also be “launch other stuff” section that looks for userscripts to be executed on kodi quit in the mediacenter script in upstream OSMC. But that would need to be discussed with the main devs.

I think I have an idea how to accomplish this. I will try something…

I wasn’t a big fan of modifying the mediacenter script mainly because of possible breakage due to OSMC updates. I also noticed that your install script didn’t make a “backup copy” of the mediacenter script, so I wound up doing that first, just in case. :smile:

Looking forward to see if you come up with another way.

You could get away with just one extra script when using nohup and in case of emulationstation openvt.
I only have ssh access to my pi until tomorrow so I cannot test this right now. But I will report back.

But maybe I can explain the main problem a bit in case someone else tries to figure it out before me:

When you start a script that opens another program and then stops the mediacenter service, the script started from KODI will also be terminated and therefore also your application.
After the mediacenter service is stopped it executes the mediacenter scipt again though with the parameter stop to clean up before a terminal session can be opened.
This is where I start my program and another script that watches the status of emulationstation in the background and restarts KODI if emulationstation quits with nohup and openvt right now by checking if a file exists that is created by me retropie.sh script.
Confused already?
Well. This is the only way I was able to keep the launched program alive until it is shut down.

If there is a way to let a launched program grab the input from KODI, it could just stay open in the background. That might introduce other problems though like still reacting to input or using up ressources.

I think I found a good solution. Have a look at my github page to see how it works without modifying the mediacenter script.

1 Like

awesome,
The other way worked very well but this looks a lot nicer. Great job.
Thank you!

Thanks for the script mcobit! Here is a modified version for limelight:

First we need to install kdb & libopus-dev:

apt-get update
apt-get install kbd
apt-get install libopus-dev

Code of my scripts:

/home/limelight/limelight.sh:

#!/bin/sh
sudo openvt -c 7 -s -f clear
sudo openvt -c 7 -s -f echo "Running limelight from KODI"
    
# Start watchdog
sudo su osmc -c "sh /home/limelight/limelight_watchdog.sh &" &
    
# Start stream
sudo su osmc -c "nohup openvt -c 7 -s -f sh /home/limelight/stream.sh >/dev/null 2>&1 &" &
    
sudo openvt -c 7 -s -f clear
sleep 2
    
# Stop mediacenter
sudo su -c "systemctl stop mediacenter &" &
    
exit

/home/limelight/stream.sh:

sudo /sbin/modprobe snd-bcm2835
sudo java -jar /home/limelight/limelight.jar pair 192.168.x.x
sudo java -jar /home/limelight/limelight.jar stream 192.168.x.x -1080 -30fps

/home/limelight/limelight-watchdog.sh:

#!/bin/bash
sleep 60
while [ true ]; do
    VAR1="$(pgrep java)"
        if [ ! "$VAR1" ]; then
            sudo openvt -c 7 -s -f clear
            sudo openvt -c 7 -s -f echo "Limelight quit... Starting KODI."
            sleep 1
            sudo openvt -c 7 -f clear
            sudo su -c "sudo systemctl restart mediacenter &" &
            sleep 1
            exit
        else
            sleep 2
fi
done

In OSMC just set a shortcut to “System.Exec(/home/limelight/limelight.sh)”.

Hi @mcobit,

I modified the script to run a script that I wrote myself, to throw from a menu item OSMC. But I have a problem at the time of launch, as my script requires one parameter and I am not able to find the solution.

# Start stream
sudo su osmc -c "nohup openvt -c 7 -s -f sh /home/limelight/stream.sh >/dev/null 2>&1 &" &

Using the example of @miko , how I can pass a parameter to “stream.sh” script? I’ve tried different way and do not receive it:

sudo su osmc -c "nohup openvt -c 7 -s -f 'sh /home/limelight/stream.sh -a 1' >/dev/null 2>&1 &" &
sudo su osmc -c "nohup openvt -c 7 -s -f -- sh /home/limelight/stream.sh -a 1 >/dev/null 2>&1 &" &
sudo su osmc -c "nohup openvt -c 7 -s -f "sh /home/limelight/stream.sh -a 1" >/dev/null 2>&1 &" &

Thank you very much, would greatly value your help.

How does the stream.sh look like?

Edit found it :wink:

See here for how to pass parameters/variables to bashscripts.

In a nutshell:

The first argument passed to a bash script will be in the variable 1.

so if you run your script with

./stream.sh hello

,then

 echo "$1"

in your script will return:

hello

Edit2:

So in your case:

if you want to pass a parameter to the jar file:

sudo java -jar /home/limelight/limelight.jar pair 192.168.x.x -a $1

The parameter for the script would then be:

./stream.sh 1

If -a should have the parameter 1