I have a Python service that I've created using PyInstaller and run with NSSM. The service works well, but I'm facing an issue with a specific functionality: opening an external executable in another process, killing the caller by stopping the service, and ensuring that the new executable continues running even after the service is stopped.
I'm using the following code to launch the external executable:
import subprocess
subprocess.Popen("myexecutable.exe", creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
The problem is that when I stop the service using (from myexecutable.exe):
subprocess.run(['sc', 'stop', 'MyService'])
the external executable also gets terminated. This behavior only occurs when the application is running as a service. When I run it by double-clicking or from the console, this issue doesn't occur.
Why is this happening, and how can I resolve it?
I suspect it might be related to the context in which the service is running or the way processes are created in this context. Any insights or solutions would be greatly appreciated.