0

I've got a little python app that I used pyInstaller on to create an exe file:

import subprocess

try:
    taskCommand = 'tasklist /FI "ImageName eq pc-client.exe"'
    reply = subprocess.Popen(taskCommand, stdout = subprocess.PIPE).communicate()[0]
    for line in reply.split("\n"):
        if line.startswith("pc-client.exe"):
            PID = line.split()[1]
            print PID
except:
    pass

try:
    killCommand = ("TASKKILL /f /t /PID " + PID)
    subprocess.Popen(killCommand, stdout = subprocess.PIPE).communicate()[0]
except:
    pass

try:
    print "Restarting Papercut Client..."
    subprocess.Popen(r"\\server\path\to\file\filename.exe", stdout = subprocess.PIPE).communicate()[0]
except:
    pass

sys.exit()

When the exe is run, it opens up in the windows command window, will run its code (it's non-interactive) and then I want the window to dissappear when its finished!

What should I put at the end of my python code to make the window close when completed. I've tried os.quit(), os._exit(), sys.quit() & sys.exit() but none of them actually close the window!

As I'm creating an exe from my code, should I use something else? I can't compile with the noconsole flag, as it needs it to actually run the commands...

Thanks.

user3514446
  • 77
  • 3
  • 10

1 Answers1

0

You can block the windows command prompt from coming up at all by putting

console=False

in your spec file. For example:

exe = EXE(pyz,
      a.scripts,
      exclude_binaries=True,
      name='YourApp',
      console=False,
      icon='IMGFolder\game_icon_cs6_icon.ico')

If you are running from the command line without a spec file, use the noconsole flag:

pyinstaller.py --noconsole yourscript.py

for more info on the PyInstaller options: https://pyinstaller.readthedocs.io/en/stable/usage.html#options

If you want the prompt to come up then close, sys.exit() works for me, but without seeing more code it will be hard to tell.

UPDATE

Since subprocesses are involved, keep in mind that the command prompt will remain open until all processes are resolved (parent and child).

It looks like you are killing the first task with its PID (I would print out the PID before you kill it to confirm you have the right one). Then you run the second task (the main file you want run?) but never kill it.

Are you trying to close the command prompt but keep the .exe running?

There is some good information about killing processes here: How to kill a python child process created with subprocess.check_output() when the parent dies?

The4thIceman
  • 3,709
  • 2
  • 29
  • 36
  • Ah, that makes sense... So can I kill off the parent process (my code) once the child process (the .exe) has started, leaving it running? – user3514446 Oct 13 '17 at 10:12
  • Yeah, but the command window won't go away because you spawned the exe "from the prompt" via a subprocess call. Unless I am missing something or misinterpreted the objective. – The4thIceman Oct 13 '17 at 16:23