0

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.

  • Leaving the terminal open can be achieved with `screen`. You need to read the generated files from disk and include them into your report email. The generation of log files does not depend on the calling method. – Paebbels Apr 19 '16 at 23:38
  • Well, I managed to solve the problem, I've been looking for the answer and my mistake was quite obvious. To run Gaussian on the terminal I use `$ g09 input.com &`, so it runs in bg. So that was the command I've been using on `subprocess.call(['command']` and `os.system(command)`. The problem was the `&`, somehow `os.system()` doesn't wait for the process to end with it. By not using `&`, it worked out. (Still can't use `subprocess.call()`) – Daniel Watanabe Apr 19 '16 at 23:48

1 Answers1

0

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 i.e., 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).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Sorry, I didn't mean to suggest that, I were using `command.split()` in order to give the command as a list. – Daniel Watanabe Apr 24 '16 at 14:49
  • @DanielWatanabe the split alone won't help here. `g09` knows nothing about `&` (there is no shell to interpret `&`). You should have seen an error on `g09` start. – jfs Apr 24 '16 at 14:56
  • I understand that now, what I don't get is that it did run the command. To test if it was running at the time I made an input that would give a specific error message after a few minutes, and I received that message, which for me was enough to say it runs but doesn't give me the log file expected – Daniel Watanabe Apr 24 '16 at 15:25
  • But thanks, now I understand why the "&" won't work and it was quite obvious. As I said in previous comments, I had managed to solve it with `os.system()`. Now I edited my script to use `subprocess.call()`cutting off the `&`and running the command in the terminal as `$ script.py input.com &` it works! – Daniel Watanabe Apr 24 '16 at 15:42