1

I have a python daemon built to count the lines of a file once every minute, do some math and calculate the number of lines added in the last minute. The output is a basic csv with the count and a timestamp so that I can latter graph it with another script.

The daemon runs for a period of time, "it seems to be between and hour or two", and then quits. There's no indication as to why.

The basic daemon template is derived from: This post

import os
import subprocess
import time
from daemon import runner
import datetime

inputfilename="test_data"

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/counter.pid'
        self.pidfile_timeout = 5
    def run(self):

        counter1 = 0
        counter_log = 0
        while True:

            count = 0
            output = open("tpm_counter.log", 'a')            

            FILEIN = open(inputfilename, 'rb')
            while 1:
              buffer = FILEIN.read(8192*1024)
              if not buffer: break
              count += buffer.count('\n')
            FILEIN.close(  )

            counter_log =  (count - counter1) * 30

            now = str(datetime.datetime.now())

            counter1 = count

            line = str(counter_log)
            output.write(line)
            output.write(", ")
            output.write(now)
            output.write("\n")

            output.close( )

            # set the sleep time for repeated action here:
            time.sleep(60)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

Does anyone have any idea why this might be quitting?

Community
  • 1
  • 1
secumind
  • 1,141
  • 1
  • 17
  • 38
  • 1
    How are you launching it? Try redirecting both stdout and stderr to a file and see if it logs something before existing. For example, if using bash shell, the command would be python d.py 2>&1 > /tmp/my.log – MK. May 20 '12 at 04:04
  • I'm starting it with "python mydaemon.py start" I've gone ahead and redirected it, we'll see what happens the next time it quits. – secumind May 20 '12 at 04:24

1 Answers1

2

Other than an exception, you have one reason the loop would end and thats this line:

if not buffer: break

I suggest you log this line before you break and then start tracing it back from there.

Update
I was looking at the code a little more closely and overlooked the inner loop. There is nothing that would break the outer loop, so I would guess it must be an exception. A super quick suggestion would be to just wrap your entire loop in a try-except and log anything that occurs:

try:
    while True:
        ...
except Exception, e:
    # log e
    raise
jdi
  • 90,542
  • 19
  • 167
  • 203
  • Ok, I've set up a second instance running on another system with this in mind, again I'll see what happens after an hour or two – secumind May 20 '12 at 04:30
  • I've finally figured this out, there's a nasty cron job running every two hours. The cron calls a bash script to restart another python script and calls for "killall python" which of course also kills this daemon. Thanks for your help. – secumind May 21 '12 at 02:49