2

I need to call Popen command from my Django app it works on development machine but it is not executing when I try to call that command when application is served by Apache no error either it just finishes silently.

def backup():
    filename = datetime.datetime.now().strftime("%d-%b-%Y_%I-%M-%S-%p")
    Popen(
        'pg_dump --dbname=postgresql://postgres:secret@127.0.0.1:5432/db -f D:/backup/%s' % filename,
    shell=True
    )

Can someone please tell me what is wrong .

Vaibhav Jain
  • 5,287
  • 10
  • 54
  • 114

1 Answers1

2

This post helped me.

I have to modify the call to Popen a bit now I'm invoking psql by full path

Popen(
        r'"C:\Program Files (x86)\PostgreSQL\9.5\bin\pg_dump.exe" --dbname=postgresql://postgres:secret@127.0.0.1:5432/db -f D:/backup/%s' % filename, shell=True
    )

Everything works fine now

Community
  • 1
  • 1
Vaibhav Jain
  • 5,287
  • 10
  • 54
  • 114
  • You're not using the shell, so you shouldn't use `shell=True`. – Eryk Sun Feb 29 '16 at 15:46
  • Instead of hard coding the path, accord to [this discussion](http://www.postgresql.org/message-id/476ACD22.5020300@postgresql.org), you can look up the `"Base Directory"` value in the registry key `Software\PostgreSQL\Installations\`. I'd enumerate the `Installations` key in both the 64-bit and 32-bit views to find the target or latest version: HKLM with `KEY_WOW64_64KEY` access and HKLM with `KEY_WOW64_32KEY` access. – Eryk Sun Feb 29 '16 at 15:49
  • @eryksun I have tried that `shell=False` it works on command prompt but it doesn't work on Apache as it is working a background process . . . it throws error `win 32 file not found` – Vaibhav Jain Feb 29 '16 at 17:52
  • You should try again, or look deeper into this, because at first glance it makes no sense. Maybe an older version of the script was cached? FYI, the cmd shell first tries `CreateProcess`, which is also what `subprocess.Popen` calls. Using the shell should only be required either for built-in shell commands or to `ShellExecuteEx` a file if `CreateProcess` fails. The latter is required to run files that aren't batch scripts or PE executable images, or when the manifest of an executable requires a more elevated access level than that of the current process token. – Eryk Sun Feb 29 '16 at 18:15
  • Sure I will look it into this. this is the error `FileNotFoundError: [WinError 2] The system cannot find the file specified` I got every time I tried running the script with `shell=False` – Vaibhav Jain Feb 29 '16 at 18:22