Running a motion detection code in virtual env only runs for a few seconds and terminates

  • Platform information:
    • Hardware: Raspberry PI 2
    • OS: Raspbian Stretch
    • Java Runtime Environment: 1.8.0_151
    • openHAB version: 2.1
  • Issue of the topic: python opencv script gets cut off after a few seconds
    I am using a webcam to get the video stream and loop over the frames to compare and detect motion. It runs perfectly fine on the usual terminal. I have changed the openhab to start as root.
    //Rule
    rule “motion.detection_switch"
    when
    Item Motion received command
    then
    if(receivedCommand==ON){
    executeCommandLine(”/home/pi/.virtualenvs/cv/bin/python /home/pi/Desktop/motionDetection.py")
    }
    end
    The logs only state the change of switch state. No errors on both events and openhab log.

executeCommandLine is not intended to be used to run something for an indefinite amount of time. If the script does not return after a certain amount of time OH assumes the script failed or got stuck and kills it.

You can provide a timeout to executeCommandLine but note that this will block until the script returns or the timepout expires and that will kill your script as well. So this timeout will need to have a long timeout which introduces its own problems.

So for to really solve this problem correctly, you need some service outside of OH that can receive a command from OH to run your script and the script needs to report its results back to OH (if needed, you are dropping the results right now so maybe you don’t care). You can’t really do it from OH.