Server scripts for help

Write a script: a second collection results ifconfig an appended to a file. A run is normal, but after a period of operation, file you can see I print time, ifconfig results did not write. How to solve the appeal, the code as follows:
#!/usr/bin/env python
from datetime import *
import time
import subprocess

def genLog():
gathertime = datetime.now().strftime('%Y%m%d_%H%M%S')
try:
f=open('/tmp/pylog/rs_conn_log_test','a+')
result = subprocess.Popen('/sbin/ifconfig',stdout=subprocess.PIPE,shell=True).stdout.readlines()
f.write(gathertime)
f.write('\n')
for line in result:
line = line.strip()
f.write(line)
f.write('\n')
finally:
f.close()

while True:
genLog()
time.sleep(1)

Started by Primo at February 02, 2016 - 9:49 PM

Approximately how long to run a problem?

Posted by Ted at February 14, 2016 - 10:02 PM

This uncertainty bug is not suitable for debugging, can only see the output log, the proposed addition of except error output (or remove try finally, it will have error output), and then look at the output of the error log analysis,
If the program must be running in the background and then joined the logging output.

Posted by Christy at February 25, 2016 - 10:56 PM

You may have
  1. subprocess.Popen('/sbin/ifconfig',stdout=subprocess.PIPE,shell=True).stdout.readlines()
Returns a null value.
Is not recommended to use finally directly, it is best to use the except to the abnormal output, which can determine where there may be a problem.

Posted by Thomas at March 01, 2016 - 11:10 PM

I run for half an hour, the acquired data is normal (12.04, Ubuntu test system)

Posted by Ted at March 04, 2016 - 11:11 PM

I added except to try it

Posted by Primo at March 06, 2016 - 11:41 PM

I go back and have a look

Posted by Primo at March 12, 2016 - 12:28 AM