每24个小时,每X秒钟运行一次功能,持续X秒钟

时间:2019-02-07 05:40:41

标签: python datetime timer setinterval argparse

我需要建立一个系统,使用户可以设置一个计时器,该计时器在24小时内每隔x小时关闭x分钟(最好从0600开始或至少在实际时间的00秒开始)

下面是我要执行的代码示例,问题是当它到达00:00(午夜)时,它又回到准备就绪状态。

import time
import argparse
from datetime import datetime 

def main(start_time_hour, start_time_minute, watering_duration, 
watering_interval_hour, watering_interval_minute):

# Watering loop, each cycle consists one application of water per watering 
cycle.
while True:

    # Sleep until start time is reached. Resets every 24 hours.
    current_time = datetime.now()
    while current_time.hour < start_time_hour:              # This loop will only break after start hour is reached,
        time.sleep(1)                                       # at which point it will only pass for remainder of 24h
        current_time = datetime.now()
        print('Not Ready - Hour')
        while current_time.minute < start_time_minute:     # This loop will only break after start minute is reached,
            time.sleep(1)                                   # at which point the watering routine will execute for
            current_time = datetime.now()                   # the remainder of the 24 hour period
            print('Not Ready - minute')
    while current_time.hour <= start_time_hour and current_time.minute < start_time_minute:              # This loop will only break after start hour is reached,
        time.sleep(1)                                       # at which point it will only pass for remainder of 24h
        current_time = datetime.now()
        print('Not Ready - minute')

    # Commence watering loop
    print("call function to turn sprinkler valve relay ON here")
    print('On time:', datetime.now().time().strftime('%H:%M:%S'))
    time.sleep(watering_duration)
    print("call function to turn sprinkler valve relay OFF here")
    print('Off time:', datetime.now().time().strftime('%H:%M:%S'))
    time.sleep(watering_interval_hour * 3600 + watering_interval_minute * 60 - watering_duration)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Watering timer code. Takes user input args and prints events to stdout")
parser.add_argument('start_time', metavar='start_time %s', help='start time in HHMM format with leading zeroes')
parser.add_argument('watering_duration', metavar='watering_duration %s', help='watering duration in seconds')
parser.add_argument('watering_interval', metavar='watering_interval %s', help='watering interval in HHMM format with leading zeroes')
args = parser.parse_args()

main(int(args.start_time[0:2]), int(args.start_time[2:4]), int(args.watering_duration),
     int(args.watering_interval[0:2]), int(args.watering_interval[2:4]))

预先感谢!

1 个答案:

答案 0 :(得分:0)

对于自动调度程序作业,

Linux crontab 是一个不错的选择。 crontab是您要定期运行的命令列表, 以及用户定义的函数/程序将在规则的时间间隔内执行。

用法:

MIN HOUR DOM MON DOW CMD

* * * * * /usr/local/bin/action-trigger.sh 

MIN :分钟字段0到59

小时:小时字段0到23

DOM :1月1日至31日

MON :月份字段1-12

DOW :星期0-6日

CMD :命令任何要执行的命令