iro/dump_test_log.py
changeset 302 3f4bdea2abbf
parent 90 eb04ac3a8327
parent 301 d5ebbcccc41b
child 303 9708742ff89c
equal deleted inserted replaced
90:eb04ac3a8327 302:3f4bdea2abbf
     1 import time, os, signal
       
     2 LOG_FILE = 'test.log'
       
     3 
       
     4 log_file = open(LOG_FILE, 'a')
       
     5 
       
     6 def log(msg):
       
     7     log_file.write(msg + '\n')
       
     8     log_file.flush()
       
     9 
       
    10 def SigUSR1Handler(signum, frame):
       
    11     print "Reacting on USR1 signal (signal 10)"
       
    12     global log_file
       
    13     log_file.close()
       
    14     log_file = open(LOG_FILE, 'a')
       
    15     return
       
    16 
       
    17 def init():
       
    18     if os.path.isfile('/var/usr/dump_test_log.pid'):
       
    19         print 'Have to stop server first'
       
    20         os.exit(1)
       
    21     else:
       
    22         print 'Starting server...'
       
    23         #write process id file
       
    24         f = open('/var/run/dump_test_log.pid', 'w')
       
    25         f.write(str(os.getpid()))
       
    26         f.flush()
       
    27         f.close()
       
    28         print 'Process start with pid ', os.getpid()
       
    29 
       
    30     signal.signal(signal.SIGUSR1, SigUSR1Handler)
       
    31 
       
    32 def main():
       
    33     init()
       
    34     count = 1
       
    35     while True:
       
    36       log('log line #%d, pid: %d' % (count, os.getpid()))
       
    37     count = count + 1
       
    38     time.sleep(1)
       
    39 
       
    40 if __name__ == '__main__':
       
    41     main()
       
    42