How to launch and run external script in background? [duplicate] - python

This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 9 years ago.
I tried these two methods:
os.system("python test.py")
subprocess.Popen("python test.py", shell=True)
Both approaches need to wait until test.py finishes which blocks main process. I know "nohup" can do the job. Is there a Python way to launch test.py or any other shell scripts and leave it running in background?
Suppose test.py is like this:
for i in range(0, 1000000):
print i
Both os.system() or subprocess.Popen() will block main program until 1000000 lines of output displayed. What I want is let test.py runs silently and display main program output only. Main program may quie while test.py is still running.

subprocess.Popen(["python", "test.py"]) should work.
Note that the job might still die when your main script exits. In this case, try subprocess.Popen(["nohup", "python", "test.py"])

os.spawnlp(os.P_NOWAIT, "path_to_test.py", "test.py")

Related

How to run external program after python program executes [duplicate]

This question already has answers here:
Launch a totally independent process from Python
(2 answers)
Closed 1 year ago.
When i try to run something like
os.system('start')
or
os.system('start file.txt')
or
os.startfile('file.txt')
it does start console, notepad or whatever, but when my python program executes, those opened programs close as well.
So my question is: Is there a way to do things like above, but without closing those programs along with my script?
It will work with frunction from the subprocess module.
Here is an example with Popen :
from subprocess import Popen
p = Popen(['notepad.exe'])
You can also use call
import subprocess
subprocess.call(['calc.exe'])

What is the best way to run a child process for limited time [duplicate]

This question already has answers here:
Run a process and kill it if it doesn't end within one hour
(5 answers)
Using module 'subprocess' with timeout
(31 answers)
Closed 4 years ago.
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!
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.)

Python to call another python without waiting for it to complete using os or subprocess [duplicate]

This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 7 years ago.
I have a python script. Which is running as a service with a while loop for ever. The script need to be executed by another python but without waiting for the out put it should pass through.
So the main script with while loop is as follows "main.py". Which never going to be end.
while True:
# do some task
time.sleep(5)
This need to be executed by another python "start.py" with similar function as follows.
os.system("main.py 1")
OR
subprocess.Popen("python main.py")
Problem here "start.py" won't finish as witing for the out put of "main.py". But I want to make it like "start.py" need to load "main.py" and leave it in background. Then the "start.py" need to complete the process. How can I modify the
os.system("main.py 1")
function to skip the waiting for "main.py"? Please consider this need to run on cross platform.
I recommend for you to check out Plumbum https://plumbum.readthedocs.org/en/latest/ specifically the section on background/foreground https://plumbum.readthedocs.org/en/latest/#foreground-and-background-execution
from plumbum import BG
from plumbum.cmd import python
python('main.py', '1') & BG
Cross platform also.

subprocess.Popen not printing/running properly [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 7 years ago.
I have a script that reads from external sensors (and runs forever), when I run it as ./zwmeter /dev/ttyUSB0 300 it behaves normally and prints output continuously to stdout. I am using bash on Ubuntu. I'd like to execute this command as part of a python script. I have tried:
from subprocess import Popen, PIPE
proc = Popen(['./zwmeter', '/dev/ttyUSB0', '300'], stderr=PIPE, stdout=PIPE)
print proc.communicate()
but I get a program that runs forever without producing any output. I don't care about stderr, only stdout and have tried splitting up the printing but still with no success.
Thank you for any help you can provide!
I think the problem has to do with the process I'm calling not terminating. I found a good work around on this site:
http://eyalarubas.com/python-subproc-nonblock.html

How to run a subprocess in the background python [duplicate]

This question already has answers here:
How to start a background process in Python?
(9 answers)
Closed 7 years ago.
I'm writing an application in python that initiates a JVM in java by calling a shell script using a python subprocess. However, my problem is that the correct way I have it written, the JVM starts and blocks the rest of the processes that happen after it. I need the JVM to run while I'm calling another function, and I need to stop the JVM after the process has finished running.
Python code:
process = subprocess.Popen('runJVM.sh', shell=True, stderr=subprocess.STDOUT)
process.wait()
r = Recommender()
r.load()
assert len(sys.argv) > 1, '%d arguments supplied, one needed' %(len(sys.argv)-1)
print "recommendations" + str(r.get_users_recommendation(sys.argv[1:]))
....
def get_users_recommendation(self, user_list):
j_id_list = ListConverter().convert(class_list, self.gateway._gateway_client)
recs = self.gateway.entry_point.recommend(j_id_list)
return recs
Where:
from py4j.java_gateway import JavaGateway
self.gateway = JavaGateway()
I can't get the get_user_recommendations to run because the JVM server is blocking the process. How do I not make it block the rest of the Python script and then kill it once the python methods have finished running and returned a value? Much thanks.
process.wait() is blocking your process. Remove that line and the rest of the code will run.
Then you can make it end by calling Popen.terminate()
Popen is invoking CreateProcess, that means the OS launches a new process and returns the PID to be stored on your process variable.
If you use process.wait(), then your code will wait until the recently created process ends (it never ends in this case unless you terminate it externally).
If you do not call process.wait(), your code will continue to execute. But you still have control over the other process and you can do things like process.terminate() or even process.kill() if necessary.

Categories