0

I am trying to do something in Python which is extremely simple - but without success. I want to run an executable from within python, such that when the calling python script exits - the exe that has been started continues to run. That is - to run the exe within an independent process - there does not need to be any inter-process communications between this process and the process which creates it.

The current target os in Windows 10 (not linux, so linux/unix solutions don't apply), but ideally a solution would be platform independent. I have tried 2 different approaches: using subprocess.Popen() and os.system()

I have not been able to get any of the 'obvious' solutions to work, or those suggested in various posts on the same subject. A list of various attempts given below - commented out. Here notepad is just being used for testing - the actual executable is different and takes command line args.

None of these work - when the python script exits (within VS Code), the notepad disappears.

import os
import subprocess

# attempts using popen()
# ----------------------
popen_args = [r'C:\Windows\System32\Notepad.exe']

# subprocess.Popen(popen_args)

# subprocess.Popen(popen_args, shell=True)

# subprocess.Popen(popen_args, creationflags=subprocess.DETACHED_PROCESS)

# subprocess.Popen(popen_args, creationflags=subprocess.CREATE_NEW_CONSOLE)

# subprocess.Popen(popen_args, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, shell=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, close_fds=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, close_fds=True, shell=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, start_new_session=True)

# subprocess.Popen(popen_args, start_new_session=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, start_new_session=True, close_fds=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, start_new_session=True, close_fds=True, shell=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# subprocess.Popen(popen_args, shell=True, stdout=None, stdin=None)

# subprocess.Popen(popen_args, shell=True, stdout=None, stdin=None, start_new_session=True)

# subprocess.Popen(popen_args, close_fds=True, stdout=None, stdin=None, start_new_session=True)

# subprocess.Popen(popen_args, close_fds=True, stdout=None, stdin=None, start_new_session=True, creationflags=subprocess.DETACHED_PROCESS  | subprocess.CREATE_NEW_PROCESS_GROUP)

# attempts using os.system()
# --------------------------
# os.system(r'START "" C:\Windows\System32\Notepad.exe')

# os.system(r'START /b "" "C:\Windows\System32\Notepad.exe"')

interestingly - running the system 'START ...' commands within a cmd console does result in notepad continuing after the host cmd console is closed.

I have done similar operations in quite a few different languages - but hit a brick wall trying to do this in python.

update

  • one comment linked to a post that suggested using the 'start_new_session' option - im not sure if this is linux/unix only - but either way it doesn't work. I have updated the list above to include these new failed attempts - none work.
  • also based on another comment - I should make clear this needs to be non-blocking, doing this with a blocking call is easy - but not what i need. the calling process needs to continue after the exe is launched, and exit properly, with the exe still running.
andrewj
  • 1
  • 1
  • Does this answer your question? [Leave a process running after closing python](https://stackoverflow.com/questions/50573169/leave-a-process-running-after-closing-python) – Ritik Mishra Aug 16 '21 at 03:35

1 Answers1

0

Tested and proved:-

import os
os.system("notepad")

This is working fine for me.

user16436946
  • 89
  • 1
  • 1
  • 10
  • os.system("notepad") is a blocking call - so the calling process waits until notepad is closed before continuing - so that is not appropriate for this situation. – andrewj Aug 16 '21 at 04:34