简体   繁体   English

使用subprocess.call()运行程序时,如何获取程序创建的日志文件?

[英]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,这是一个用于分子几何优化的程序,其中包括其他应用程序。 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. 高斯可能需要几天的时间才能结束一次优化,所以我决定在Python上编写一个程序,以便在程序运行完毕后向我发送电子邮件。 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. 问题是高斯自动生成一个日志文件和一个chk文件,其中包含该过程的实际结果,并且通过使用subprocess.call(['command'], shell=False)不会生成这两个文件。

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. 我还尝试使用os.system(command)解决该问题,它给了我.log文件和.chk文件,但是发送电子邮件时没有等待优化完成。

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. 通过使用subprocess.call(['command'], shell=False)不会生成两个文件。

Your comment suggests that you are trying to run subprocess.call(['g09 input.com &'], shell=False) that is wrong. 您的评论表明您正在尝试运行subprocess.call(['g09 input.com &'], shell=False)

Your code should raise FileNotFoundError . 您的代码应引发FileNotFoundError If you don't see it; 如果您没有看到它; it means stderr is hidden. 这表示stderr已隐藏。 You should fix it (make sure that you can see the output of sys.stderr.write('stderr\\n') ). 您应该对其进行修复(确保可以看到sys.stderr.write('stderr\\n') )。 By default, stderr is not hidden ie, the way you start your parent script is broken. 默认情况下, stderr不隐藏,即启动父脚本的方式已损坏。 To be able to disconnect from the session, try: 为了能够断开会话,请尝试:

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

or use screen , tmux . 或使用screentmux

shell=False (btw, it is default—no need to pass it explicitly) should hint strongly that call() function does not expect a shell command. shell=False (顺便说一句,它是默认值,无需显式传递它)应强烈暗示call()函数不希望使用shell命令。 And indeed, subprocess.call() accepts an executable and its parameters as a list instead—it does not run the shell: 实际上, subprocess.call()接受可执行文件及其参数作为列表,而不运行外壳程序:

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). 注意:如果g09返回非零退出代码(通常指示错误),则check_call()会引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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