Crontab以特定间隔获取脚本列表

时间:2018-10-03 10:21:17

标签: python cron

我们如何获取使用python crontab在特定时间(例如4:00 PM到8:00 PM)之间在crontab中运行的脚本的列表 我正在获取该用户在crontab中运行但无法在该特定时间运行的所有作业的列表。

这样尝试:

获取所有脚本的列表。

from crontab import CronTab
import datetime
import croniter

cron = CronTab("user")

for j in cron:
   print j

在特定的时间间隔尝试

from crontab import CronTab
import datetime
import croniter

cron = CronTab("pfmuser")

for j in cron:
    hour = int(j.split()[1])
    minutes = int(j.split()[0])
    if hour >= 16 and hour <= 20:
        print('fond a job running between 16 and 20')

得到错误:AttributeError:“ CronItem”对象没有属性“ split”

发生错误,但

***but now i have:
1) 10 0 * * 5
2)  0 3 1 * *
3)  0 0 * * *
4)  0 1 * * *
5)  0 10 * * * 

now if have some problem and the cron scripts didnt run then if i am running for 0-10th hour then i need only 

3)  0 0 * * *
4)  0 1 * * *
5)  0 10 * * *  
these scripts only should run 

1) 10 0 * * 5  (only friday)
2)  0 3 1 * * (only 1st month)
these should not run 

how to  keep  this may be can i use datetime and get that particular details later compare with the cron info.How do i go*** 

具有以下建议的编辑脚本:

from crontab import CronTab
import datetime
from croniter import croniter as ci

cron = CronTab("pfmuser")

for cronitem in cron:

    cronitemstr = str(cronitem)
    hour = cronitemstr.split()[1]
    minutes = cronitemstr.split()[0]
    minutesint = None
    hourint = None
    try:
        hourint = int(hour)
        minutesint = int(minutes)
    except ValueError as e:
        print e.message
    if hourint >= 16 and hourint <= 20:
        cronitemstr = cronitemstr[:cronitemstr.index(cronitemstr.split()[5])]

        min=cronitemstr.split()[0]
        dom=cronitemstr.split()[2]
        mon=cronitemstr.split()[3]
        dow=cronitemstr.split()[4]
       date_time=datetime.datetime.now()
        try:
           if(min=='*' and dom=='*' and mon=='*' and dow=='*' ):
                print cronitemstr
                print cronitem
           else:
                print "none"
        except Exception as e:
            print "e"

需要在if子句中包含更多条件,因为我们需要甚至包含

  • 16 * * 3:dis是根据今天的星期四第16点工作,因此该脚本也应显示
  • 16 * * 1-5:表示星期一,星期二,星期三,星期四,星期五,它应该运行,因此也应该显示它。
  • 16 * 10 *:表示需要执行的第10个月的第16个小时中的每一天。因此这也应显示 这样,我们需要检查许多条件。据我说,我认为我需要考虑今天的日期,如datetime.date.today(),它需要与cron表达式进行比较。 当前* 16 * * *表示分钟,天,月的第16小时的所有脚本。这仅是一个实例,需要考虑所有需要包含的脚本

1 个答案:

答案 0 :(得分:1)

cronitem对象不是字符串,因此没有属性split。您可以在尝试拆分之前将其转换为str。例如:

from crontab import CronTab
import datetime
from croniter import croniter as ci

cron = CronTab("pfmuser")



for cronitem in cron:

    cronitemstr = str(cronitem)
    hour = cronitemstr.split()[1]
    minutes = cronitemstr.split()[0]
    minutesint = None
    hourint = None
    try:
        hourint = int(hour)
        minutesint = int(minutes)
    except ValueError as e:
        print e.message

    print hourint
    print minutesint
    if hourint >= 16 and minutesint <= 20:
        print('found a job running between 16 and 20')

        cronitemstr = cronitemstr[:cronitemstr.index(cronitemstr.split()[5])]
        iter = ci(cronitemstr)
        next_run = iter.get_next()
        last_run = iter.get_prev()
        next_run_datetime = datetime.datetime.fromtimestamp(next_run)
        last_run_datetime= datetime.datetime.fromtimestamp(last_run)
        print 'the next run is at {}'.format(next_run_datetime)
        print 'the last run was at {}'.format(last_run_datetime)