Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell command from python

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

like image 738
Vaidyanathan Avatar asked Dec 05 '25 10:12

Vaidyanathan


2 Answers

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)
like image 146
Saullo G. P. Castro Avatar answered Dec 06 '25 23:12

Saullo G. P. Castro


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

like image 29
Jonathan Mayer Avatar answered Dec 07 '25 01:12

Jonathan Mayer