0

I'm looking to control a script via Zigbee/XBee using X-CTU. I've created a script called zb_control.py. Now I'm trying to start and stop another script within this script. A script adxl345test.py is used to collect data from an attached accelerometer on my Raspberry Pi.

The idea behind the zb_control.py script is that I run it and then if I type "run" in X-CTU the script will start running adxl345test.py and collect data.

I've figured out how to start a script within a script however I can't get it to work when I try to stop the adxl345test.py again.

As you can tell I've tried different things:

import serial, time, sys, os, subprocess
from subprocess import check_call
from subprocess import call

while True:

    ser=serial.Serial('/dev/ttyUSB0',9600,timeout=2)
    inc=ser.readline().strip()

    if inc=='run':
        print("---------------")
        print("Collecting data”)
        print("---------------")
        call(["python", "adxl345test.py"])

    elif inc=='stop':
        # check_call(["pkill", "-9", "-f", adxl345test.py])
        # serial.write('\x03')
        # os.system("pkill –f adxl345test.py")
        # call(["killall", "adxl345test.py"])
        print("-----------------------")
        print("Script has been stopped")
        print("-----------------------")

Any suggestions on how I could stop the adxl345test.py script from running and then still have the zb_control.py running ready to recieve new input from X-CTU?

user75374
  • 21
  • 4
  • 3
    I think call executes it synchronously, so your main script is going to be held there until it completes. Maybe look in to doing something like this https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true – kevswanberg Jan 10 '18 at 15:59
  • Possible duplicate of [How to terminate a python subprocess launched with shell=True](https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – Daniel Pryden Jan 10 '18 at 18:47

0 Answers0