I am trying to fork() and exec() a new python script process from within a Django app that is running in apache2/WSGI Python. The new python process is daemonized so that it doesn’t hold any association to apache2, but I know the HTTP ports are still open. The new process kills apache2, but as a result the new python process now holds port 80 and 443 open, and I don’t want this.
How do I close port 80 and 443 from within the new python process? Is there a way to gain access to the socket handle descriptors so they can be closed?
If you use the
subprocessmodule to execute the script, theclose_fdsargument to thePopenconstructor will probably do what you want:Assuming they weren’t simply closed, the first three file descriptors are traditionally
stdin,stdoutandstderr, so the listening sockets in the Django application will be among those closed in the child process.