简体   繁体   English

在python中创建和删除cron作业

[英]Creating and deleting cron jobs in python

So I have a little script I wish to run once a week. 所以我有一个小脚本,希望每周运行一次。 It will check on some variable and if its set, it continues running the script, if not, I want it to wait an hour and try again. 它将检查一些变量,如果已设置,它将继续运行脚本,如果没有,我希望它等待一个小时,然后重试。 If it's still not set, it'll wait 2 hours, then 4, and then give up for the week. 如果仍未设置,则将等待2个小时,然后等待4个小时,然后放弃一周。 My question is, can I do this in python? 我的问题是,我可以在python中这样做吗? Seems like I'd have to create and delete cron jobs in python to get this to work. 似乎我必须在python中创建和删除cron作业才能使其正常工作。

You can't really set standard crons directly from Python. 您不能真正直接从Python设置标准crons。 Instead, I'd set the cron to fire every hour, and in the code determine if it needs to run again (ie last successful execution is more than 7 days ago). 相反,我将cron设置为每小时触发一次,并在代码中确定是否需要再次运行(即,上一次成功执行是在7天之前)。

You can try using Time.sleep 您可以尝试使用Time.sleep

if not_set:
    Time.sleep(60 * 60)        # 1 hour
if not_set:
    Time.sleep(60 * 60 * 2)    # 2 hours
if not_set:
    Time.sleep(60 * 60 * 4)    # 4 hours

I believe that you are looking for the at command, you can find a little tutorial here: http://www.ibm.com/developerworks/linux/library/l-job-scheduling/index.html 我相信您正在寻找at命令,可以在这里找到一些教程: http : //www.ibm.com/developerworks/linux/library/l-job-scheduling/index.html

Basic usage: 基本用法:

at -f some_file_with_the_jobs.sh some_time

You can easily use relative times: 您可以轻松地使用相对时间:

at -f some_file_with_the_jobs.sh  10pm tomorrow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM