Hopefully someone can help me. I am new-ish to python/programming.
I have a script which controls when another script gets called for an alarm system.
Basically i want to call tryingToSpawn.py if two vars in a database are set. If two different vars are set I want to kill/terminate tryingToSpawn.py. But I am having problems getting it to work.
The first subprocess is to establish if a phone is present on the LAN. This works perfectly! It is the second subprocess seems to work too. Its just when I try to stop it, it fails. The code and error is below
This is the code:
Code: Select all
import subprocess from subprocess import Popen, PIPE import signal import sys import os try: # while loop checks status before the if statements are checked while True: # pull data from DB - SQL for row in records: #list of vars # determine the location of the a mobile result = subprocess.check_output("sudo nmap -sn 192.168.0.0-50", shell = True) if (mac in result): deviceLocation = 1 elif (mac not in result): deviceLocation = 2 # if statements control the actions of the script if (routineStatus == 1) and (deviceTriggerLocation == deviceLocation): if componentsid == 5 and routineAction == 1: proc1 = subprocess.Popen( "exec" 'python tryingToSpawn.py', shell=True) if componentsid == 5 and routineAction == 2: os.kill(proc1.pid, signal.SIGINT) if routineStatus == 0: os.kill(proc1.pid, signal.SIGINT) mySQLconnection.close() sys.exit() # encase DB fails - trace error except Error as e: print("Error while connecting to oh_Database-:", e) # closing resources finally: if(mySQLconnection.is_connected()): mySQLconnection.close()
I am just trying to run this from terminal. This is the error i get:
Traceback (most recent call last):
File "routineDevice.py", line 156, in <module>
NameError: name 'proc1' is not defined
I think I do understand that for some reason 'proc1' is out of scope as a var, inside the other if statements. Can anyone help, suggest a different way of doing this? I just need to have the parent script launch a child python script (the child needs to run in the background without hanging up the parent script) and then the parent needs to be able to stop the child using a few if statements.
Is this possible?