我正在尝试在python中构建cron作业计划程序,该计划程序从数据库中获取计划并使用crontab库在linux中创建cron作业。但是当尝试添加希望从数组中指定cron运行的星期几时,我无法弄清楚如何绕过“ Unrecognised Week of Day:”错误。我如何使用数组进行此操作的任何帮助都将非常有帮助。
from crontab import CronTab
from datetime import datetime
cron=CronTab('pi')
#the below line simulates grabbing the data from the database
row={'starttime':'2:56:00','sun':1,'mon':1,'tue':0,'wed':0,'thu':1,'fri':0,'sat':1}
#convert the "starttime" into the hour and minute to use on the cron
d=datetime.strptime(row['starttime'],'%H:%M:%S')
hour=d.strftime('%H')
minute=d.strftime('%M')
#figure out what day of week I want the cron to run on
dow=[]
if(row['sun'] == 1):
dow.append("Sun")
if(row['mon'] == 1):
dow.append("Mon")
if(row['tue'] == 1):
dow.append("Tue")
if(row['wed'] == 1):
dow.append("Wed")
if(row['thu'] == 1):
dow.append("Thu")
if(row['fri'] == 1):
dow.append("Fri")
if(row['sat'] == 1):
dow.append("Sat")
#create the job
job=cron.new(command='python /home/pi/Documents/blinktest.py', comment='Schedule')
job.minute.on(minute)
job.hour.on(hour)
job.dow.on(dow) #<- this is the line I get the "unrecognised day of week" error
#write the cron
cron.write()
这是我得到的错误:
ValueError: Unrecognised Day of Week: '['Sun', 'Mon', 'Thu', 'Sat']'
答案 0 :(得分:0)
如果其他人遇到同样的问题,如注释中提到的user1438644,则将代码从
更改job.dow.on(dow)
到
job.dow.on(*dow)
成功了。