Skip to content

Instantly share code, notes, and snippets.

@JustinAzoff
Last active December 25, 2015 05:39
Show Gist options
  • Select an option

  • Save JustinAzoff/6926443 to your computer and use it in GitHub Desktop.

Select an option

Save JustinAzoff/6926443 to your computer and use it in GitHub Desktop.
checks to see if a file is growing and exits accordingly. Useful with a cron job
#!/usr/bin/env python
# */5 * * * * root sleep 60 ; is_file_changing /usr/local/bro/logs/current/conn.log || broctl restart
import os
import sys
import time
SIZE_TIMEOUT = 10
def get_size(f):
for x in range(SIZE_TIMEOUT):
try:
size = os.stat(f).st_size
return size
except OSError:
time.sleep(1)
return os.stat(f).st_size
def is_growing(f):
size = get_size(f)
time.sleep(1)
for x in range(SIZE_TIMEOUT):
time.sleep(1)
newsize = get_size(f)
if newsize != size:
return True
return False
def is_growing_with_retries(f):
"""Check to see if a file is growing, with retries
this should handle the case when a file gets rotated mid check
"""
for x in range(3):
if is_growing(f):
return True
return False
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage %s filename" % sys.argv[0]
sys.exit(1)
f = sys.argv[1]
res = is_growing_with_retries(f)
if not res:
print "%s is not growing" % f
sys.exit(not res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment