2

I'm making a python script (start.py) to run multiple (4) python scripts. My code:

    import subprocess
from time import sleep

y=(0.2)
sleep (y)
subprocess.Popen(["python", 'a1.py'])
sleep (y)
subprocess.Popen(["python", 'a2.py'])
sleep (y)
subprocess.Popen(["python", 'a3.py'])
sleep (y)
subprocess.Popen(["python", 'a4.py'])

When I run start.py the four scripts run in background as I expected, but each one with a process ID. Is it possible to have one PID for all?

And how can I make the start.py run at startup as a service? (i'm using raspberry pi).

8
  • 1
    is there a reason to run the scripts as subprocesses instead of just importing them and running corresponding functions? Commented Sep 17, 2015 at 12:55
  • The really old school way is to write each script so that it execve()s the next, and in your main script begin with execve()ing the first ;) This way you simply replace processes so it's the same PID all over. Commented Oct 2, 2015 at 22:36
  • @CongMa: if you can modify the scripts then you should modify them to be able to import them instead e.g., import a1, a2, a3, a4; a1.a1(), a2.a2(), ... (actual names should reflect what modules/functions do) Commented Oct 3, 2015 at 2:09
  • @J.F.Sebastian But recursion! Unix! Aaargh! Commented Oct 3, 2015 at 10:20
  • 1
    I kinda expected that, Stack Overflow being what it is, someone will come up with an answer that uses os.execve() in each script, so the process gets replaced recursively. I tried to pre-empt that ;) Sorry for lowering the already low signal-to-noise ratio. Commented Oct 4, 2015 at 14:14

2 Answers 2

3

To run the Python script inline within the same interpreter you can use execfile:

https://docs.python.org/2/library/functions.html#execfile

Python 3 equivalent:

What is an alternative to execfile in Python 3?

To start a script as a background service it is best to use external tool like Linux's systemd or supervisord for this purpose.

Sign up to request clarification or add additional context in comments.

Comments

-2

you can try this code:

import subprocess
from time import sleep
import sys
y=(0.2)
sleep(y)
subprocess.Popen([sys.executable, 'a1.py'],stdin=subprocess.PIPE)
sleep(y)
subprocess.Popen([sys.executable, 'a2.py'],stdin=subprocess.PIPE)
sleep(y)
subprocess.Popen([sys.executable, 'a3.py'],stdin=subprocess.PIPE)
sleep(y)
subprocess.Popen([sys.executable, 'a4.py'],stdin=subprocess.PIPE)

i recommaned to execute script one by one if all think is good then you can execute above program

1 Comment

(1) each Popen() does spawn a new process i.e., your code contradict the title of the question (2) if you want to wait for each script to finish then use subprocess.check_call() to run it. (3) you should consider importing the corresponding modules instead of executing them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.