Python with tcpdump in a subprocess: how to close subprocess properly?
Instead of p.kill()
, you can use p.send_signal(subprocess.signal.SIGTERM)
to send a terminate signal rather than a kill (p.terminate()
does the same).
The Popen docs describe the send_signal()
command. The documentation on signals is a bit weak, but a dir(subprocess.signal) will list all the signals you may send to the process, but terminate should allow it some time to clean up.
Found working solution:
I've changed p.kill()
to p.terminate()
.
After this change the subprocess
is "properly" finished (output of tcpdump
subprocess with statistics available in console) and output .pcap
file not damaged.