首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按特定时间间隔获取脚本列表的Crontab

按特定时间间隔获取脚本列表的Crontab
EN

Stack Overflow用户
提问于 2018-10-03 10:21:17
回答 1查看 547关注 0票数 1

我们如何使用python获得在crontab中运行的脚本列表--比如4:00PM到8:00PM --我得到的是用户在crontab中运行的所有作业的列表,但在那个特定的时间无法得到。

尝试过这样做:

得到所有脚本的列表。

代码语言:javascript
复制
from crontab import CronTab
import datetime
import croniter

cron = CronTab("user")

for j in cron:
   print j

试图在那个特定的时间之间:

代码语言:javascript
复制
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‘对象没有属性’拆分‘

错误已完成,但

代码语言:javascript
复制
***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*** 

编辑脚本,并提出以下建议:

代码语言:javascript
复制
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 :表示mon,tues,wed,thur,this,它应该运行,所以这也应该显示。
  • 16 * 10 *:意味着每天在第10个月的16小时,它需要execute..so,这也应该显示像这样,我们需要检查许多条件。据我所知,我认为我需要考虑今天的日期,比如datetime.date.today(),从那开始,我需要与think表达式进行比较。实际上,* 16 ***意味着所有的脚本在16小时,每天,month.this将仅仅是一个例子,需要考虑所有需要包括的
EN

回答 1

Stack Overflow用户

发布于 2018-10-03 12:00:50

cronitem对象不是字符串,因此没有属性拆分。在你尝试分裂之前,你可以把它投到一个str上。例如:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52624875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档