【问题标题】:Have python run script at X:00 am [duplicate]让 python 在 X:00 am [重复] 运行脚本
【发布时间】:2011-04-15 09:28:42
【问题描述】:

后续,

With python: intervals at x:00 repeat

使用线程,如何让脚本在上午 8:00 开始运行,在下午 5:00 停止运行

解决方案应该在 python 中编码,并且是可移植的

tiA

【问题讨论】:

  • 这是作业吗?如果是这样,请添加适当的标签。
  • 这不是作业,我有在 SQL --trigger 中可以正常工作的解决方案,现在需要 python 解决方案。

标签: python windows multithreading time solaris


【解决方案1】:

time 模块有一个名为asctime 的函数,它可能对您有用:

>>> from time import asctime
>>> asctime()
'Tue Sep 21 17:49:42 2010'

因此,您可以将以下内容合并到您的代码中:

sysTime = asctime()
timestamp = systime.split()[3]
separator = timestamp[2]
hour = timestamp.split(separator)[0]
while hour < 8:
    # just wait
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]

# now, it's just become 8:00 AM
while hour < 17: # until 5:00 PM
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]

    # start your thread to do whatever needs to be done

启动这个脚本一次,让它一直运行。

这是对@user428862 询问是否可以使用“小时> 8 和小时

while 1:
    sysTime = asctime()
    timestamp = systime.split()[3]
    separator = timestamp[2]
    hour = timestamp.split(separator)[0]
    minute = timestamp.split(separator)[1]

    if (hour > 8) and (hour<17 and minute<1):
        # start your thread to do whatever needs to be done

另外,我突然想到我一直在使用字符串拆分并返回字符串,所以 hour 应该是 int(timestamp.split(separator)[0]) 等等

【讨论】:

  • 考虑到 cron 不是 OP 的选项,因此它是可移植的。
  • VBA 和 Sql 中的逻辑相同,谢谢。脚本从上到下运行的频率。 “小时> 8和小时
  • 用“(小时
【解决方案2】:

在 cron 中,您需要从上午 8:00 开始运行脚本,在下午 5:00 停止运行 在 linux 中使用 crontab -e 命令。 并为

添加此行代码
* 8 * * * /YOUR/PATH/SCRIPT

你在下午 5 点停止它,在这个例子中,我们将在下午 5 点杀死所有 python 进程

* 17 * * * killall -9 /usr/bin/python

您可以使用 crontab -lcrontab -r 检查 crontab 是否重置为默认值(不会执行任何命令)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2013-01-02
    • 2018-01-08
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多