I’m running a program on raspberry pi which opens few other python programs for a certain amount of time and then closes them. What is happening:
- if I’m running the main program from e.g. Python 3 IDE or Thonny Python or other IDEs, everything works fine, it opens and closes all programs as expected.
- if I’m running the main program from e.g. Python 3 IDE or Thonny Python or other IDEs, but I have some other terminal window opened (not even running anything, just opened and sitting idle), it will open all the programs, but it’s unable to close them down after.
- if I’m running the main program in terminal window, it will open all the programs, but it is unable to close them down after.
code e.g.
while True:
proc = subprocess.Popen(['lxterminal', '-e', ' test_aa.py'])
proc_bb = subprocess.Popen(['lxterminal', '-e', 'test_bb.py'])
time.sleep(5)
proc.terminate()
proc_bb.kill()
time.sleep(200)
Another example
while True:
print("opening")
proc = subprocess.Popen(['lxterminal', '-e', ' test_aa.py'])
c = proc.pid
print(c)
time.sleep(3)
os.kill(c, signal.SIGTERM)
time.sleep(700)
works if no other terminal window opened manually,
To start the programs I also tried different variations of Popen and os.system, they work every time fine.
To kill/terminate I’ve tried: os.system, os.kill(pid, signal.SIGTERM), os.kill(pid, signal.SIGINT) etc. kill with -9 PID, subprocess.Popen and then kill() or terminate()
UPDATE 1
As per my question above, I think I can partially answer it, but at the same time this will raise more questions.
Taking under consideration this code:
while True:
print("opening")
proc = subprocess.Popen(['lxterminal', '-e', ' test_aa.py'])
c = proc.pid
print(c)
time.sleep(3)
os.kill(c, signal.SIGTERM)
time.sleep(700)
using
ps aux
I found that
['lxterminal']
And
['test_aa.py']
Are running with different pid numbers. So
c = proc.pid
is giving me pid for ['test_aa.py'] and this probably could explain why I can’t terminate ['lxterminal']
Am I correct in saying that?
What would be other options to start a python program in another terminal window, but to make sure they have same pid numbers?
UPDATE 2
I'm running the new terminal window under one pid number – so that’s sorted. I’m also using code to make sure there is no zombie process running:
stdout, stderr = proc.communicate()
proc.wait()
and it does work, no zombie process running after.
But unfortunately same story again, if I open some terminal window and run my program it won’t terminate terminal started by my program. I tried on two different raspberry pi. Same thing.
Why if I have another terminal window opened, it suddenly fails to close my programs?
FINAL UPDATE
I’m partially answering my own question
I still don’t understand why if I had some terminal window opened my program wasn't terminating other programs, but I found a way around it. Instead of
lxterminal,
I am using
uxterm #uterm can also be used
My code looks like this now:
proc = subprocess.Popen([‘uxterm’, ‘-e’, ‘test_aa.py’], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid)
it work perfect. I can have other programs running in terminal windows, I can reboot and using .sh files bring all programs back to life again.
This is exactly what I was looking for :-)
Thanks everyone for help.
Another example:
while True: print("opening") proc = subprocess.Popen(['lxterminal', '-e', ' test_aa.py']) c = proc.pid print(c)
#works if no other terminal window opened manually, if opened by main program no problems to close down.
– samson77 Jan 06 '20 at 23:52Just edited the main question – samson77 Jan 07 '20 at 06:30