#!/usr/pkg/bin/python """parse vixie-cron-style /etc/cron.d/* file and run it as specified user you should run this file from a "normal" cron job in /var/spool/cron/root (every minute). """ Copyright = """ cronparse - run /etc/cron.d/ files on "normal" unix system Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.sep.join([pwd.getpwuid(os.geteuid())[5], 'lib', 'python'])) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): pass # defined instead by pytest module, use that for debugging # other globals, specific to this program import time crondir = '/etc/cron.d' minmax = [[0, 59], [0, 23], [1, 31], [1, 12], [0, 6]] validpatterns = ['\*', '\*/[0-9]+', '[0-9]+', '([0-9]+,)+[0-9]+'] validpattern = '^(' + '|'.join(validpatterns) + ')$' unixweekday = [1, 2, 3, 4, 5, 6, 0] # python uses Monday=0, unix uses Sunday=0 def cronparse(): """main routine, does the job of parsing and excuting crontabs from /etc/cron.d/ directory """ try: files = os.path.os.listdir(crondir) except: sys.exit(1) ignore, month, day, hour, minute, second, weekday, ignore, ignore = \ time.localtime() weekday = unixweekday[weekday] for file in files: DebugPrint('%s:' % file) crontab = open(os.path.join(crondir, file), "r") for line in crontab.readlines(): try: DebugPrint(line) items = line.split() if len(items) < 7: continue for index in range(0, len(items)): DebugPrint(items[index]) if index < 5: if not re.compile(validpattern).match(items[index]): raise Exception, "no match" for except: DebugPrint('Continuing past exception') continue if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... cronparse() else: # if you want something to be done on import, do it here; otherwise pass pass