0

I am making a new script(p.py)for my Raspberry Pi and i am trying to combine 2 python scripts named Final.py and A.py . I want to execute the Final.py for about 5 seconds and then kill it and proceet to A.py, run it for 25 seconds and kill it and repeat the whole process endlessly. However, both of the scripts are looped and i don't know how to end these processes. Here the p.py code:

import subprocess
import time
execfile('Finaaal.py') #looped script 1
time.sleep(5.0)
subprocess.call(['./1.sh']) #this kills Finaaal.py
execfile('A.py') #looped script 2
time.sleep(25.0)
subprocess.call(['./2.sh']) #this kills A.

Does anyone know any better ideas or a better way to fix this? Everything would be greatly appreciated!

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Tony
  • 5
  • 4

1 Answers1

1

What you're trying to do doesn't really make any sense. You run the sub-scripts like this:

execfile('Finaaal.py') #looped script 1

But execfile just runs that script in the current interpreter, not as a separate program. Which means there's no way script 1.sh could possibly kill Finaaal.py without also killing the controller script.

The answer is simple—you've already imported subprocess, you just need to use it to run the sub-scripts instead of using execfile. And then you don't even need 1.sh and 2.sh scripts; you can just kill the processes directly:

import subprocess
import sys
import time

p = subprocess.Popen([sys.executable, 'Finaaal.py'])
time.sleep(5.0)
p.kill()
p = subprocess.Popen([sys.executable, 'A.py'])
time.sleep(5.0)
p.kill()

Since you've tagged your question both python-2.7 and python3.x, you probably need to think about which Python should be used to run the sub-scripts. Using sys.executable means it'll be the same one used to run the controller script, which is usually what you want (but if not, obviously do something different there, whether that's "hardcode python3" or "whatever the shbang says" or whatever's appropriate).

(Since you appear to be on POSIX, you can even modify Finaaal.py and A.py to catch the SIGKILL and do some clean shutdown if you want. Or, if you want a hard shutdown instead of having that option, use terminate instead.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • then `subprocess.Popen([sys.executable, 'Finaaal.py'])` part is worth an upvote, reusing the current interpreter to run sub-scripts. – Jean-François Fabre Mar 30 '18 at 20:26
  • @Jean-FrançoisFabre Probably not worth an upvote until I actually explain it, though… I'll edit it. – abarnert Mar 30 '18 at 20:42
  • don't explain _me_, I got it :) – Jean-François Fabre Mar 30 '18 at 20:42
  • So it worked thanks!!! Do you also know how to loop this p.py script ? – Tony Mar 30 '18 at 20:44
  • @Jean-FrançoisFabre I meant explain it to the OP, not to you. :) – abarnert Mar 30 '18 at 20:45
  • @Tony The same way you loop anything else—e.g., take the whole thing and indent it under a `while True:`. – abarnert Mar 30 '18 at 20:45
  • Thank you so much! I rreally appreciate helpfull people like you! – Tony Mar 30 '18 at 20:47
  • 1
    @Tony Also, this question has been closed as a dup. This means there's an existing question (two of them, in fact) that have answers that should apply to your problem, and that have been upvoted by multiple people over time. So definitely go read them; they've probably covered things I haven't. – abarnert Mar 30 '18 at 20:47
  • 1
    @abarnert another good reason to use the python executable is when using Windows. Just using the script name doesn't cut it, because windows doesn't care about shebangs... you'd have to use `shell=True` (eeeek!!) or `cmd /c ` prefix (not portable). `sys.executable` rocks because it's super-portable (well, except if the script is frozen using `cx_freeze` or `py2exe`) – Jean-François Fabre Mar 30 '18 at 20:57
  • @Jean-FrançoisFabre Yeah, I purposely didn't explain what to do for that particular "something different" case, because… it's hairy, and probably irrelevant for the OP. If you have a use case that's specifically about Windows, usually the answer is something to do with `py.exe`. – abarnert Mar 30 '18 at 20:58