在Ubuntu16.04上,如何重复运行python脚本 - 使用crontab

时间:2017-07-24 15:36:35

标签: python cron

我想在Ubuntu 16.04上使用crontab重复运行python脚本

我在终端上运行以下命令。

1 * * * * python /home/elite/python/weather.py

并写下如下。

weather.py

我认为,这确实意味着每分钟运行一次weather.py脚本。

这是用于测试crontab功能的from urllib import urlopen import time import re testing = 'testing' current_time = time.localtime() today = time.strftime('%Y-%m-%-d-%-s', current_time) file_name = today + ".txt" output = open("/home/elite/python/" + file_name, "w") output.write(testing) 脚本。

$python weather.py

当我在终端上运行此脚本 - crontab时,它运行良好。

但{{1}}似乎无效。

我该如何处理?

2 个答案:

答案 0 :(得分:1)

开头的1表示小时的分钟,因此应该每小时运行一次,而不是每分钟运行一次。

如果您想每分钟运行一次,则需要* * * * *

答案 1 :(得分:1)

两件事。

首先,Crontab喜欢命令的绝对路径。例如/usr/bin/python。输入which python以获取绝对路径。

其次,1 * * * *每小时一次。我喜欢使用https://crontab.guru来帮助我完成正确的时间。

相关问题