Skip to content

Instantly share code, notes, and snippets.

@jul
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jul/10024786 to your computer and use it in GitHub Desktop.
Save jul/10024786 to your computer and use it in GitHub Desktop.
listing task scheduled in 1 hour around current time works for me (tm)
from croniter import croniter as ci
import os
from datetime import datetime as dt
from time import time
from itertools import takewhile
from functools import reduce
from os import walk
before = 3600.0
after = 3600.0
import re
_now = time()
_pjoin = lambda *a: reduce(lambda x,y:os.path.join(x,y), a)
def hms(s):
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
return '%d:%02d:%02d' % (hours, minutes, seconds)
def look_task(fn, before, after):
crontab_split = [ x for x in map(
lambda l: re.split("\s+",l),
filter(
lambda m: re.match("^\s*(\*|\d+)",m),
open(fn).readlines()
)
)]
other_view= { " ".join(x[5:]) : ci(" ".join(x[0:5]), _now - before) for x in crontab_split }
for cmd, when in other_view.items():
has_next = when.get_next()
print "*" * 80
print "CMD:%s:%s" % (fn,cmd)
while has_next < _now + after:
strf = has_next < _now and "last %s ago" or "next in %s "
print strf % hms(abs( _now - has_next))
has_next = when.get_next()
print "=" * 80
def get_f_in(_dir):
files = [ x for x in os.listdir(_dir) ]
return [ x for x in map(lambda f:_pjoin(_dir, f), files) ]
cron_list = [ "/etc/crontab" ]
cron_list += get_f_in("/etc/cron.d") + get_f_in("/var/spool/cron/crontabs")
for f in cron_list:
look_task(f, before, after)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment