You can use os.kill method to terminate the process.
os.kill(tcpdumpProcess.pid, signal.SIGTERM)
Вопрос
I need to run tcpdump
, kill it after a bit and read from stderr.
The following code works in Python 2.7, but I need to make it work in Python 2.5 too::
tcpdumpProcess = subprocess.Popen(['sudo', '/usr/sbin/tcpdump',
'-w', 'dumpedTraffic.pcap',
'-n', 'ip'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
tcpdumpProcess.terminate()
tcpdumpProcess.wait()
tcpdump_stderr = tcpdumpProcess.communicate()[1]
Python 2.5 complains that:
tcpdumpProcess.terminate() AttributeError: 'Popen' object has no attribute 'terminate'
What's en equivalent way of doing it in Python 2.5?
Решение
You can use os.kill method to terminate the process.
os.kill(tcpdumpProcess.pid, signal.SIGTERM)
Другие советы
You can execute the Kill command along with processid as same as u execute the previous command.
or
u can use the kill methods provided in the os module along with the process id and the signal status such as TERM etc.