[Tutor] os command

Hugo González Monteverde hugonz-lists at h-lab.net
Thu Oct 27 00:07:38 CEST 2005


os.system will return the errorval of the application. You need to

1) get the pid of the child process
2) kill it using os.kill(os.SIGTERM)
3) reap the killed process

This is all in unix/linux, of course.

what I do (untested, please check order of args and correct usage of exec):

pid = os.fork()

if pid == 0: #child process
     os.execvp("tcpdump", "tcpdump", "-n",  "-i",  "eth0")

else:   #parent
     os.kill(pid, os.SIGTERM)
     os.waitpid(pid, 0)   #wait for process to end

Johan Geldenhuys wrote:
> I have script that calls a system command that I want to run for 5 minutes.
> """
> import os
> cmd = 'tcpdump -n -i eth0'
> os.system(cmd)
> """
> I can start a timer after the cmd is issued, but I don't know how to 
> send a control signal to stop the command after I issued it. This is 
> normally from the shell  ^c.
> This cmd my run in more than one thread on different interfaces and I 
> don't whant all of then stopped at once. I think the best way is to make 
> a thread for each interface where the cmd can be issued and stopped 
> without the danger of stopping he wrong thread.
> Can anybody help?
> Thanks
> Johan
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list