简体   繁体   English

创建一个计时器以定期运行程序

[英]Creating a Timer to run a program periodically

I've created a program which edits a .txt file. 我创建了一个程序来编辑.txt文件。 This .txt file is used as a source of data for our website. 该.txt文件用作我们网站的数据源。 These data change every hour. 这些数据每小时更改一次。 So, how can I make the python program run automatically every hour? 那么,如何使python程序每小时自动运行? I'm just a beginner, sorry. 我只是一个初学者,对不起。

If its windows 如果它的窗户

Windows Task Scheduler Windows Task Scheduler

For command-line usage, you can schedule with the at command . 对于命令行用法,可以使用at命令进行计划 I'll add the schtasks command which is the replacement in newer MS OS. 我将添加schtasks命令,它是更新的MS OS中的替代命令。

If its unix base then you can use cron . 如果其unix为基础,则可以使用cron

For Linux and Unix, use crontab command to schedule your Python script. 对于Linux和Unix,请使用crontab命令安排您的Python脚本。

http://www.computerhope.com/unix/ucrontab.htm http://www.computerhope.com/unix/ucrontab.htm

For Windows, use "Scheduled Task" 对于Windows,请使用“计划任务”

http://drupal.org/node/31506 http://drupal.org/node/31506

On Windows you can use Task Scheduler. 在Windows上,您可以使用任务计划程序。 Unfortunately, i t's not immediately obvious how to create a task that repeats with a time period since it is normally used to schedule things by a time of day, date, day of week, or system state. 不幸的是,由于通常用于按一天中的某个时间,日期,一周中的某天或系统状态来计划任务,因此如何立即重复执行某个任务并不是很明显 Here are the instructions to set up a periodic task. 以下是设置定期任务的说明。

  1. Create a new task. 创建一个新任务。 Here you can put the command to run, whether that is an executable, batch file, or other command. 无论是可执行文件,批处理文件还是其他命令,都可以在此处运行该命令。
  2. Open the new task's properties. 打开新任务的属性。
  3. Go to the Triggers tab 转到触发器选项卡
  4. Add a new Trigger 添加一个新的触发器
  5. Set when you want the task to first run ("At log on" is a good idea if it needs an interactive user). 在您希望任务首次运行时进行设置(如果需要交互式用户,则“登录时”是个好主意)。
  6. If you want it wait a bit, you can specify a delay in minutes 如果要稍等一下,可以指定延迟时间(以分钟为单位)
  7. Check "Repeat task every:" 选中“每重复一次任务:”
  8. In the drop down, you can select some values, or type your own. 在下拉菜单中,您可以选择一些值,也可以键入自己的值。 Note that on most versions of windows, you can only specify it in terms or minutes, hours, or days. 请注意,在大多数版本的Windows中,只能以术语或分钟,小时或天来指定它。 The range of allowed values is between 1 minute and 31 days. 允许值的范围是1分钟到31天。
  9. Set the duration (Indefinitely if you want it to run as long as the computer is on. 设置持续时间(如果您希望计算机在开机时一直运行,则可以无限期设置。
  10. Click OK 单击确定

任务计划程序的新触发器对话框

Congrats! 恭喜! You can poke around the other settings to see things like how it will handle task failures and other things. 您可以在其他设置中四处查看,以了解诸如处理任务失败的方式之类的内容。

Hope this helps! 希望这可以帮助!

The best way is with a cron job - It is a unix system that is made for running scripts at certain times. 最好的方法是使用cron作业-这是一个UNIX系统,专门用于在特定时间运行脚本。

Check out http://en.wikipedia.org/wiki/Cron for more information. 请访问http://en.wikipedia.org/wiki/Cron了解更多信息。 Hourly is a pretty easy one to set up. 每小时很容易设置。

This way, all your program does is the logic (updating the txt file), and you dont need to write the hourly code. 这样,您的程序所要做的就是逻辑(更新txt文件),而您无需编写每小时的代码。

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

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