[Tutor] why "ifconfig" is alway running?
lei yang
yanglei.fage at gmail.com
Sun Dec 19 06:46:29 CET 2010
#!/usr/bin/env python
import datetime
import subprocess
import sys
import os
import signal
from time import sleep
def runForAWhile(cmd, secs=10):
print("running %s" % cmd)
timeout = datetime.timedelta(seconds=secs)
print timeout
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
status = proc.poll()
start = datetime.datetime.now()
while (status is None and (datetime.datetime.now() - start) <
timeout): #not timed out
print proc.stdout.readline() #TODO timestamp?
#print status
#print datetime.datetime.now() - start
if 0 == status:
print("'%s' is program exited" %cmd)
else:
try:
os.kill(proc.pid, signal.SIGINT)
print "Process timeout: '%s'" % cmd
except OSError:
pass
cmd="ping 128.224.165.20 -c 4"
runForAWhile(cmd,30)
I see that "status" always "!=0“ why program is NOT exited
Lei
On Sun, Dec 19, 2010 at 1:15 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "lei yang" <yanglei.fage at gmail.com> wrote
>
>> def runForAWhile(cmd, secs=10):
>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
>>
>> stderr=subprocess.STDOUT, shell=True)
>> status = proc.poll()
>> start = datetime.datetime.now()
>> while (status is None and
>
> (datetime.datetime.now() - start) < timeout): #not timed out
>>
>> print status
>> if 0 == status:
>> print("'%s' is program exited" %cmd)
>> else:
>> try:
>> os.kill(proc.pid, signal.SIGINT)
>
>> why it print many "None" in 10 second. which means "ifconfig" is
>> running in 10sec, why, what's wrong withi my script,
>
> You only check the status once *before* entering the while loop.
> You need another status check inside the loop.
>
>> I just want to let my programe running, if it's timeout(10sec), kill it
>
> Its not clear from your mail if you have checked whether the
> process really is running - using the OS commands 'top' or 'ps'
> for example or whjether its just the None result you are concerned
> about. But try updating status first...
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list