[英]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,请使用“计划任务”
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. 以下是设置定期任务的说明。
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.