Kodi Community Forum

Full Version: Detecting if a Python service is running?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

Basically, I have a service script that I need to detect if it's running or not.
Therefore, I need something I can set - which is always reset when XBMC starts.

Was thinking of using:
xbmc.executebuiltin("Skin.SetBool(script_running)")
inside the service.

Then, in my other script checking if this was set.
But, result = xbmc.executebuiltin('Skin.HasSetting(script_running)') always returns None

The service will also be shutdown unexpectedly, so it doesn't know when it closes.

Thanks,

Matt
Of, if XBMC had a method same as os.getpid() (get process id) that worked across all platforms?
Or, some other value that changes on startup but remains constant?
ok its not super easy, but not very difficult to implement and it works on any platform:

1. create a tcp server in your service (maybe inside a seperate thread)
2. from another addon you can ask the server if it is alive or timeout if not

http://docs.python.org/2/library/socket.html
http://pymotw.com/2/socket/tcp.html
You can also periodically save a time stamp to a file or database. Check the timestamp from the other add-on. If the timestamp is older than the interval, the service has died.
(2013-07-01, 19:52)Bstrdsmkr Wrote: [ -> ]You can also periodically save a time stamp to a file or database. Check the timestamp from the other add-on. If the timestamp is older than the interval, the service has died.

Rather than saving to file or database you could just save it as an XBMC window property, these would automatically be cleared when XBMC restarts, ie

Code:
xbmcgui.Window(10000).setProperty("My_Service_Timestamp", timestamp)
Hi,

That's what i'm looking for, a window property that get's cleared.

I wouldn't need timestamp then.

Simply do xbmcgui.Window(10000).setProperty("serviceRunning") in the service.

Then, in other script - check if this setting is set...

The service should only not be running when the script is first installed.

I noticed when you disable then reenamble an addon with a service - the service starts.
It's only when you first install the script, the service does not start until a restart.

This is the only case I want to check.

So, my main default.py will just be.

Code:
If window property "serviceRunning" not set:
      start service .py
Open script settings
Or if the user disabled the service ;)
If the service has been disabled, then they won't be able to get to the default.py anyway?

My structure is:

addon folder
- default.py (just runs service if not running then opens the addon settings dialog)
- service.py
One thing to note is that the setProperty needs to set a value, so you will need:

Code:
xbmcgui.Window(10000).setProperty('My_Service_Running', 'True')

and then:

Code:
if xbmcgui.Window(10000).getProperty('My_Service_Running') != 'True'
      start service .py
Open script settings

I would also suggest using a property name that is guaranteed not to have been used by another developer, so maybe put your addon name at the start?
Yup.

Using below in my service.py script:

Code:
xbmcgui.Window(10000).setProperty(ADDON_ID + '_running', 'True')

DO SERVICE STUFF

xbmcgui.Window(10000).setProperty(ADDON_ID + '_running', 'False')

then in my default.py:

Code:
if(xbmcgui.Window(10000).getProperty(ADDON_ID + '_running') != "True"):
     xbmc.executebuiltin('XBMC.RunScript('+path+', False)')
Nice one!

(2013-07-02, 13:24)matthuisman Wrote: [ -> ]
Code:
xbmcgui.Window(10000).setProperty(ADDON_ID + '_running', 'False')

Or just

Code:
xbmcgui.Window(10000).clearProperty(ADDON_ID + '_running')
Now...

Any ideas on how to tell if the service script is being called on-boot, or being called via another method (enable/disable script, via another script)
For another script, it's easy - just pass an argument.

However, for the script to detect if it's being started via boot or script enable is a bit trickier.

Was thinking I could set a window property of "hasrun" that get's set the first time the script is run.
Therefore, if it runs again, it knows it has already been run and therefore getting re-enabled.
However, this won't work with the situation where you disable the script, power down XBMC, power on, then re-enable script.

Any ideas?

Maybe check system up-time?
If this is greater than 10seconds, then assume the script has been enabled.
Got it:

if ( xbmcgui.getCurrentWindowId() == 10000 ):
#running from boot
else:
#not running from boot
What if the user triggers the script to start from the home screen?
Hi.

The script is a service so it runs when XBMC boots.
XBMC always defaults to home screen on boot.

So, if the service runs and detects it's on the home screen, I know the script is being run "on boot".
But, if the service runs and it's not on the home screen, I know the script has been "enabled" via the "addon information dialog"

My service continually runs, but needs to run a special function only if "on boot"
Pages: 1 2