How to prevent script from being killed by Kodi

I have a Python script that I want to run after Kodi loads and to keep running indefinitely. (The script launches a slideshow under control of an external gesture sensor wired to the Pi.)

I created an script and saved it in /home/osmc/.kodi/userdata

The script runs nicely after reboot and Kodi loads, but then terminates.

Here’s the Python code:

import os, sys, subprocess

fpid= os.fork()
if fpid!=0:
        #Running as daemon now with PID fpid; exit the parent
        sys.exit(0)"sudo /home/osmc/scripts/", shell=True)

(The shell script contains a while loop that keeps it running and checking for sensor input.)

Examining ~/.kodi/temp/kodi.log, I see the problem:

00:22:15 T:1780458528  NOTICE: -->Python Interpreter Initialized<--
00:22:15 T:1780458528 WARNING: CPythonInvoker(3): Script invoked without an addon. Adding all addon modules installed to python path as fallback. This behaviour will be removed in future version.
00:22:16 T:1676669984  NOTICE: Thread BgPicLoader start, auto delete: false
00:22:21 T:1780458528   ERROR: CPythonInvoker(0, /usr/share/kodi/addons/script.module.osmcsetting.updates/ script didn't stop in 5 seconds - let's kill it

CPythonInvoker is apparently playing nanny and killing my script.
How do I prevent this?

I should mention that I previously tried running from /etc/rc.local and also tried adding it to systemd, and I couldn’t get either of those approaches to work either. I’m just looking for the simplest way to daemonize a bash script. Anyone have any suggestions?

My solution was to go back to using /etc/rc.local. I put the command I was trying to run into that file just above the “exit 0” line and made sure that the script I was calling launched a process in background mode (i.e., it calls the program as “GestureTest &” with an ampersand at the end).

I had to put the line “sleep 15” into rc.local before I called my script to make sure it didn’t launch before Kodi is completely loaded.

Also, I found by using “sudo journalctl” to examine the system log that I could see the output of the script launched by rc.local as it ran. This helped me debug a couple problems with the script.