Built-in functions for skins

I am trying to make a shell script execute using a shortcut on my keyboard, but i have a feeling that System.Exec just doesnt work.
Is it included in OSMC ? Normal keymappings work…but i just can get System.Exec to execute scripts.

Using it like this:

...<key id="2159104">System.Exec("/etc/init.d/vncboot start")</key>...

-Thanks

First thing worth mentioning is that command should be run with root privileges. Kodi runs as the ‘osmc’ user.

Yeah thanks, i totally forgot about that.
Kodi freezes though after i execute it now.
Although the script starts the command i want it seems that the whole GUI hangs.
Executing sudo /etc/init.d/vncboot start from the console works fine though.
I’ll look more into it.

OSMC is systemd based, so you shouldn’t really be running a legacy LSB service script directly. It will work after a fashion but isn’t recommended.

Instead use the systemd service management commands such as:

sudo systemctl start vncboot.sevice

If you want to set the service to run at boot:

sudo systemctl enable vncboot.service

http://www.freedesktop.org/software/systemd/man/systemctl.html

Its not really a service…but a script. I’ve never used systemd before to tell you truth. I am a small time CentOS, Debian user…This is all Greek to me (and i am Greek lol).
I basically wanted to have a script tied to a button to start/stop vnc server. the vncboot script is just a switch basically.
I made a file called vncboot inside /etc/init.d/.
Then chmod +x /etc/init.d/vncboot
My vncboot:

#!/bin/sh
# /etc/init.d/vncboot
### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO
VNCUSER='osmc'
PROGRAMBIN="dispman_vncserver"
PIDOFPROGRAMBINCMD="pidof $PROGRAMBIN"
PIDOFPROGRAMBIN=`eval $PIDOFPROGRAMBINCMD`
PROGRAMSTR="dispman_vncserver"

eval cd ~$VNCUSER
case "$1" in
 start)
   sudo modprobe uinput
   sudo chmod 666 /dev/uinput

   su $VNCUSER -c '/usr/bin/dispman_vncserver'
   #echo "Starting dispman_vncserver server for $VNCUSER "
   ;;
 gui)
        if [ "x$PIDOFPROGRAMBIN" != "x" ] ; then
                echo "Closing $PROGRAMSTR"
                sudo ps aux | grep "dispman_vncserver" | grep -v grep | awk '{print $2}' | xargs sudo kill -9
        else
               echo "Starting $PROGRAMSTR..."
                   sudo modprobe uinput
                   sudo chmod 666 /dev/uinput
                   sudo dispman_vncserver
        fi
   ;;
 stop)
   sudo ps aux | grep "dispman_vncserver" | grep -v grep | awk '{print $2}' | xargs sudo kill -9
   #echo "dispman_vncserver stopped"
   ;;
 *)
   echo "Usage: /etc/init.d/vncboot {start|gui|stop}"
   exit 1
   ;;
esac
exit 0

Using this keymap.xml file (from /home/osmc/.kodi/userdata/keymaps)

<keymap>
    <global>
        <keyboard>
            <key id="61596">mute</key>
            <key id="61597">volumedown</key>
            <key id="2159104">RunScript(/home/osmc/vnc/vncon.py)</key>
        </keyboard>
    </global>
</keymap>

I execute a python script that executes the vncboot. (.py script located in /home/osmc/vnc/)

vncon.py

import os
os.system('sudo /etc/init.d/vncboot gui')

So to toggle vncserver on/off i press the ‘Win’ button on my remote. (id=2159104).

I’m trying to RunScript in the same fashion but it doesn’t seem to work.

I have KeyMap plugin installed and my keymap file is gen.xml (but I also tried keymap.xml which works for volumeup)

mapping keys in the files for volumeup volumedown for example works but RunScript function doesn’t.

I am running a python script which calls a simple test bash script but it never runs (if I run it manually it works)

Permissions ok, Key id ok, tested it with volumeup. I’m running out of ideas.

Any idea?

make sure the owner/group are the pi user (or osmc, cant remember…check with a console command to see the rest of the files)
Also do chmod +x YOUR_SH_FILE
and finally make sure you have sudo blah blah infront of every command like for example:

vncon.py

import os
os.system(‘sudo /etc/init.d/vncboot gui’)

because maybe the command is executed but just doesnt have the rights to really execute.

yea checked all that still not running.

also single quotes and double quotes make a difference…try switching what you have now…I think single quotes is for the python scripts, and double quotes for the keymap commands (when it comes to executing shell commands)
I ended up not using any here: RunScript(/home/osmc/vnc/vncon.py) just for that reason…in the end i removed them just to make sure.

This is what I’m testing:

keymap.xml:

<keyboard><key id="61526">RunScript(/home/osmc/test.py)</key>

test.py:

#!/usr/bin/env python
import os
bashCommand = "sudo /bin/bash /home/osmc/test.sh"
os.system(bashCommand)

test.sh:

#!/bin/sh
echo test >> ./test.txt

ls -al | grep test

-rwxrwxrwx  1 osmc osmc      105 Jun  5 23:03 test.py
-rwxrwxrwx  1 osmc osmc       36 Jun  5 23:08 test.sh
-rwxrwxrwx  1 osmc osmc       50 Jun  5 23:08 test.txt

I’m expecting the size of the file test.txt to increase when I press the letter v on my keyboard but not happening.

if I run /home/osmc/test.py it does increase though.

I tried single and double quotes but nothing.

I just realized it was the relative path I used instead of the full path for test.txt.

it is working! What was I thinking :smile:

test.sh had to be:

#!/bin/sh
echo test >> /home/osmc/test.txt

hehehe nice 1 :slight_smile:

pls i need some help to get kodi to run my key id in keyboard.xml

<key id="61597">RunScript(/home/osmc/vncon.py)</key>

the log files show the script as triggered, but i get no result.

i have confirmed that python /home/osmc/vncon.py, does the trick from the shell, but…

thx in advance,
Dam0

Are you sure that in the .py script you are running the command with sudo ?

os.system('sudo /etc/init.d/vncboot gui')

also double check that both in the py ad sh script you are using full paths.

thx for the input.

my problem appears to be the lack of bin in the path /usr/bin >> NOOb Error

strange how the command works fine from shell though? i guess kodi ignores the system paths?

thx once again,
D.

1 Like

“system paths” are a shell feature. If you execute a program directly using something like os.system() without running it inside a shell then there are no system paths, so you must specify the paths explicitly.

It’s generally good practice in these kind of circumstances to always provide the full path to the program because even if you do run the command within a shell, different users (osmc, root and so on) typically have different paths as well.

Gotcha.

thx for the tip.

D.

hmm, seems if i have vnc server running ( connected or not), when i choose reboot (havnt tried shutdown yet) kodi hangs.

ssh continues to run fine.

you can however, continue to turn off the vnc server using ssh.

once vnc server is closed, the unit will reboot as normal.

D.

Is the VNC server being run with a proper systemd unit?

using method above;

D.