This question already has answers here:
Nohup is not writing log to output file
(7 answers)
Python script writes no output when stdout is redirected to a file [duplicate]
(1 answer)
Using tee to get realtime print statements from python [duplicate]
(1 answer)
Python app does not print anything when running detached in docker
(13 answers)
Closed 2 years ago.
I have a Python script like the following:
#!/usr/bin/env python
print("Start")
sql = get_sql_from_file(...)
# The following function takes a long time to run
execute_oracle_driver(sql)
print("Finished")
I execute it with:
(my-env) $ time nohup python script.py &
Then I check if there's any initial output, but nothing:
(my-env) $ cat nohup.out
(my-env) $
I would at least expect to see "Start" after no more than a few seconds. Even after waiting a few minutes, then killing it, I get no output. If possible, how can I fix this?
Output is buffered by default, so you would need to ensure that the output is flushed; otherwise, no output will be seen in nohup.out until the end of the program. One way to do that is as follows:
print("Start", flush=True)
Related
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'])
This question already has an answer here:
Python subprocess and user interaction
(1 answer)
Closed 2 years ago.
I have a program that when I run in shell, it asks for input from the user.
When I run it as follows with subprocess.Popen, it does not get into the part of the program
where it asks for input and it just finishes. How could I change that?
cmd = ["./pairwise", "-seq", sites, "-loc", locs, "-lk", "species_genotypenew_lk.txt", "-prefix", ldhat_out]
subprocess.Popen(cmd)
Thanks!
if pairwise is reads from stdin and writes to stdout
subprocess.Popen(cmd).communicate()
should do it.
This question already has answers here:
how to direct output into a txt file in python in windows
(6 answers)
Closed 2 years ago.
I have a python program that runs command line (Windows 10), how can I save the output as a text file, also I want to run the command line process in background.
you can save the output of a command to a text file by simply typing > txtFile.txt after your command, like:
dir > text.txt
and for your task to run in the background you should use Threads in python.
example:
from threading import Thread
def do_in_background():
# your background task should be here.
pass
t = Thread(target=do_in_background, daemon=True)
t.start()
This question already has answers here:
Disable output buffering
(16 answers)
Closed last month.
I have the following code to flushing out the output buffer.
print('return 1')
sys.stdout.flush()
Can I set up the print function so that it automatically flushes the buffer when it's called?
You can start python in unbuffered mode using the -u flag, e.g.
python -u script.py
or
#!/usr/bin/env python -u
as "shebang" header for your script.
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")