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

2024-03-29 13:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在ubuntu16.04上使用crontab反复运行python脚本

我在终端上运行下面的命令。你知道吗

$crontab -e

并在上面写了如下。你知道吗

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

我认为,这确实意味着每分钟都要运行weather.py脚本。你知道吗

这是用于测试crontab功能的weather.py脚本。你知道吗

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似乎不起作用。你知道吗

我怎么处理?你知道吗


Tags: namepyimport脚本终端homeoutputtoday
2条回答

开头的1表示一小时中的一分钟,所以它应该每小时运行一次,而不是每分钟。你知道吗

如果你想每分钟都跑,你需要* * * * *

两件事。你知道吗

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

其次,1 * * * *是每小时一次。我喜欢使用https://crontab.guru来帮助我的cron时间正确。你知道吗

相关问题 更多 >