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).

georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • 1
    is there a reason to run the scripts as subprocesses instead of just [importing them and running corresponding functions](http://stackoverflow.com/q/30076185/4279)? – jfs Sep 17 '15 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. – Cong Ma Oct 02 '15 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) – jfs Oct 03 '15 at 02:09
  • @J.F.Sebastian But recursion! Unix! Aaargh! – Cong Ma Oct 03 '15 at 10:20
  • @CongMa: what do you mean? – jfs Oct 04 '15 at 04:39
  • @J.F.Sebastian Not really ;) – Cong Ma Oct 04 '15 at 14:05
  • @CongMa: I do not understand. Could you elaborate? – jfs Oct 04 '15 at 14:08
  • 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. – Cong Ma Oct 04 '15 at 14:14

2 Answers2

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.

Community
  • 1
  • 1
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
-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

Najeeb Choudhary
  • 396
  • 1
  • 3
  • 21
  • 2
    **(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.](http://stackoverflow.com/questions/32628433/executing-a-python-script-without-spawning-a-new-process-with-subprocess#comment53110640_32628433) – jfs Oct 03 '15 at 02:07