Reputation: 399
I am trying to compile a set of lines and execute them and append the output to text file. Instead of writing the same thing, I used a python script to compile and execute in background.
import subprocess
subprocess.call(["ifort","-openmp","mod1.f90","mod2.f90","pgm.f90","-o","op.o"])
subprocess.call(["nohup","./op.o",">","myout.txt","&"])
The program pgm.f90
is getting compliled using the ifort compiler, but the ouput is not getting appended to myout.txt
. Instead it is appending output to nohup.out
and the program is not running in the background even after specifying "&"
in the python script.
What obvious error have I made here?
Thanks in advance
Upvotes: 3
Views: 3516
Reputation: 1482
This issue is that when you supply arguments as a list of elements, the subprocess library bypasses the shell and uses the exec syscall to directly run your program (in your case, "nohup"). Thus, rather than the ">" and "&" operators being interpreted by the shell to redirect your output and run in the background, they are being passed as literal arguments to the nohup command.
You can tell subprocess to execute your command via the shell, but this starts a whole extra instance of shell and can be wasteful. For a workaround, use the built-in redirection functionality in subprocess instead of using the shell primitives:
p = subprocess.Popen(['nohup', "./op.o"],
stdout=open('myout.txt', 'w'))
# process is now running in the background.
# if you want to wait for it to finish, use:
p.wait()
# or investigate p.poll() if you want to check to see if
# your process is still running.
For more information: http://docs.python.org/2/library/subprocess.html
Upvotes: 4
Reputation: 59005
You can call a subprocess as if you were in the shell by using Popen()
with the argument shell=True
:
subprocess.Popen("nohup ./op.o > myout.txt &", shell=True)
Upvotes: 5