Problem with Cron launching a Python script

Hi there,

I have a small python script that checks whether a file is playing, and copies/deletes a file if there is or isn’t

I have tested the file works by assigning a remote button in remote.xml to launch the script, and it all works fine.

The command issued in remote.xml is: RunScript(/home/osmc/.kodi/userdata/pyscript/test.py)

I now want to use cron to launch the script every 5 minutes, but it does not seem to be working.

Cron is installed and working.

The cron command I have is: */5 * * * * python /home/osmc/.kodi/userdata/pyscript/test.py

I have tried putting /usr/bin/python, python and nothing before the path to the script, none of these seem to work.

Any ideas?

does it run right outta the box if you make it executable ?

It is executable, but it does not run from the command line, I believe OSMC has its own Python that runs from inside OSMC, so from the command line it produces an error (hence it does work from the remote.xml).

The code for the python script is:

#!/usr/bin/python

import xbmc
import subprocess

if xbmc.getCondVisibility("Player.HasVideo()"):
    subprocess.call( "cp ~/.kodi/userdata/pyscript/playing.file ~/media/Videos", shell=True )
else:
    subprocess.call( "rm ~/media/Videos/playing.file", shell=True )

not sure if that helps though, as I say, the script works fine when called from remote.xml, but not fro cron.

Try using FULL paths in the script rather than the ~ (home) symbol

To clarify, the script IS working, when called from remote.xml, but is not working from cron.

I have amended the file to use full paths, but this has made no difference.

When calling the script from the command line the output is:

Traceback (most recent call last):
  File "/home/osmc/.kodi/userdata/pyscript/test.py", line 4, in <module>
    import xbmc
ImportError: No module named xbmc

But I believe this is due to the internal/external python issue - externally it cannot import xbmc.

Unless cron is also outside then I can understand that it will not work, but as I understand it cron operates with the internal python.

Can anyone confirm this?

Are you sure it’s xbmc and not kodi?

I would have thought so, but I believe it is still xbmc - hence it works perfectly when called from remote.xml.

I have changed it to kodi, but then the script does not work at all, from remote.xml or cron.

edit: I should say it IS xbmc, as it does work, whereas kodi does not.

You can’t import xbmc from outside the Kodi process. You need to use the JSONRPC api instead

Also make sure you installed cron via App Store

Also run python scriptname in a shell to verify the script

Sam, thanks for the input, but I’m afraid I was lost almost immediately!

I’m afraid my coding skills (as you can probably tell from the python script!) are minimal, and I have no idea how to use the JSONRPC api - is this used in cron to launch the python script, or to replace the python script ?

Hi Sam, I appreciate you are busy - this is a hell of a project you have to keep a grip on, and ‘odd’ little personal projects like this are not what you need to be working on, so I did some research…

I found this page that has a similar goal, in that it checks that there is media playing, and pause/unpause if there is.

I have re-purposed this to simply detect that media is playing, and copy/delete my file as required (though it does seem to work the other way around!). I don’t really understand what it is doing to get the result, which is a shame, as I like to understand things.

I have to say this seems to be a much more complicated way of ding things, and I am lucky that there was an example with similar goals.

my code is now:

#!/usr/bin/python


import requests
import json
import urllib
import subprocess

headers = {'content-type': 'application/json'}

xbmc_json_rpc_url = "http://192.168.4.153:80/jsonrpc"

payload = {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}
url_param = urllib.urlencode({'request': json.dumps(payload)})

response = requests.get(xbmc_json_rpc_url + '?' + url_param, headers=headers)

data = json.loads(response.text)

if data['result']:
    subprocess.call( "cp /home/osmc/.kodi/userdata/pyscript/playing.file /home/osmc/media/Videos", shell=True )
else:
    subprocess.call( "rm /home/osmc/media/Videos/playing.file", shell=True )

I tested this from the command line, and it works fine, then it did it automatically - because the cron job kicked in!

So far so good!

1 Like

Good stuff.

You may benefit from http://kodi.wiki/view/JSON-RPC_API/v6

Sam