简体   繁体   中英

How do I get the log file a program creates when running it with subprocess.call()?

I work with Gaussian, which is a program for molecular geometry optimization, among other applications. Gaussian can take days to end a single optimization so I decided to make a program on Python to send me an e-mail when it finishes running. The e-mail sending I figured out. The problem is that Gaussian automatically generates a log file and a chk file, which contains the actual results of the process and by using subprocess.call(['command'], shell=False) both files are not generated.

I also tried to solve the problem with os.system(command) , which gives me the .log file and the .chk file, but the e-mail is sent without waiting for the optimization completion.

Another important thing, I have to run the entire process in the background, because as I said at the beginning it might take days to be over and I can't leave the terminal open that long.

by using subprocess.call(['command'], shell=False) both files are not generated.

Your comment suggests that you are trying to run subprocess.call(['g09 input.com &'], shell=False) that is wrong.

Your code should raise FileNotFoundError . If you don't see it; it means stderr is hidden. You should fix it (make sure that you can see the output of sys.stderr.write('stderr\\n') ). By default, stderr is not hidden ie, the way you start your parent script is broken. To be able to disconnect from the session, try:

$ nohup python /path/to/your_script.py &>your_script.log &

or use screen , tmux .

shell=False (btw, it is default—no need to pass it explicitly) should hint strongly that call() function does not expect a shell command. And indeed, subprocess.call() accepts an executable and its parameters as a list instead—it does not run the shell:

subprocess.check_call(['g09', 'input.com', 'arg 2', 'etc'])

Note: check_call() raises an exception if g09 returns with a non-zero exit code (it indicates an error usually).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM