вкратце:
def log(s):
dtime = time.strftime('%y/%m/%d(%w) %H:%M',time.localtime( time.time() ))
open('lsh.log','a').write('%s: %s\n' % (dtime,s))
def main():
while 1:
try:
time.sleep(3)
d = time.localtime( time.time() )
ds = time.strftime('%m/%d/%w %H:%M',d)
if ds==ds_old: continue
ds_old = ds
print ds
except Exception as e:
log('except main loop: %s' % (str(e),))
10/02/11(4) 05:17: except main loop: [Errno 9] Bad file descriptor
# coding: utf-8
import time,re,os
# cron
# sec min hour day mon wday
jobs = []
def log(s):
dtime = time.strftime('%y/%m/%d(%w) %H:%M',time.localtime( time.time() ))
open('lsh.log','a').write('%s: %s\n' % (dtime,s))
def start(dtime,com):
log(com)
print 'start:',com
# ch dir
cur = os.getcwd()
path = com
f = path.rfind('\\')
if f>=0: path = path[:f]
try:
os.chdir(path)
# start
import subprocess
subprocess.Popen(com, shell = True)
os.chdir(cur)
except:
log('except %s' % (com,))
def loadjobs(fname='lsh.lsh'):
global jobs
for s in open(fname,'r').readlines():
s = s.rstrip()
if s and s[0]!='#':
f = s.find(':')
if f:
job = s[:f]
com = s[f+1:]
if job == 'import':
if fname!=com: loadjobs(com)
elif job == '@restart': start('start',com)
else: jobs.append([job,com])
def test(d,job):
#m = [d.tm_sec, d.tm_min, d.tm_hour, d.tm_mday, d.tm_mon, d.tm_wday]
m = [d.tm_min, d.tm_hour, d.tm_mday, d.tm_mon, d.tm_wday]
d = job.split(',')
if len(d)!=5:
print 'error in job time settings'
return 0
for n,j in enumerate(d):
min = m[n]
for t in j.split(' '):
# * /2 /1
if t=='*': continue
if (t=='/1') and (min%2 == 1): continue
if (t=='/2') and (min%2 == 0): continue
# int
r = re.match(r'^(\d+)$',t)
if r and int(r.groups(0)[0])==min: continue
# a-b
r = re.match(r'^(\d+)-(\d+)$',t)
if r and ( int(r.groups(0)[0]) <= min <= int(r.groups(0)[1]) ): continue
#
return 0
return 1
def main():
ds_old = ''
while 1:
try:
time.sleep(3)
d = time.localtime( time.time() )
ds = time.strftime('%m/%d/%w %H:%M',d)
if ds==ds_old: continue
ds_old = ds
print ds
for job in jobs:
if test(d,job[0]): start(ds,job[1])
except Exception as e:
log('except main loop: %s' % (str(e),))
log('start')
loadjobs() # загрузка данных
main() # основная программа