【发布时间】:2019-07-01 00:42:42
【问题描述】:
我使用的是 xubuntu 18.01
我有一个 python 程序,它可以抓取天气数据并将文件保存为 csv。在我使用chmod +x weatherdata 授予权限后,它可以在终端中完美运行命令weatherdata。
我希望使用 cron 每 2 周运行一次。但是我设置后什么都没有发生。
我正在使用 NANO cron 编辑器
我已经尝试 PATH 变量 SHELL=/bin/bash/ 设置了 cronjob。 P 把 /bin/bash 放在命令前面……都无济于事……
# crontab -e
SHELL=/bin/bash
MAILTO= mygmail@gmail.com (removed my actual email for privacy)
PATH=/home/luke/bin/
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
34 * * * * /bin/bash /home/luke/bin/weatherdata
我预计weatherdata python 文件将在每小时 34 分钟后每小时执行一次。
什么都没有发生,我没有收到电子邮件或任何东西。
编辑:
我把顶部改成
SHELL=/bin/bash
#MAILTO=mygmail@gmail.com (commented it out)
PATH=/home/luke/bin/:/usr/sbin:/usr/bin:/sbin:/bin
跑步后我得到了什么:
15 * * * * /bin/bash /home/luke/bin/weatherdata > /tmp/weatherlog 2>&1
正如 cmets 中所建议的那样。
Traceback (most recent call last):
File "/home/luke/Documents/Duka/Master_River_Levels_LOOP.py", line 9, in <module>
import pandas as pd
ImportError: No module named pandas
Traceback (most recent call last):
File "/home/luke/Documents/Duka/Temp_Mean_Weather_loop.py", line 9, in <module>
import pandas as pd
ImportError: No module named pandas
我需要导入一些模块来运行 python 脚本,第一个是 panadas。
【问题讨论】:
-
您已从 PATH 中删除了
/bin和/usr/bin,因此任何脚本都可能表现不佳。此外,除非您已采取措施为其配置系统,否则发送到外部 gmail 地址的邮件将失败。请尝试cron tag wiki 上的故障排除指南,尤其是有关日志记录的部分:34 * * * * /bin/bash /home/luke/bin/weatherdata > /tmp/weatherlog 2>&1,并在执行后查看 /tmp/weatherlog。 -
谢谢@thatotherguy。显然 cron 没有安装 pandas 或任何其他 python 库。我不知道该怎么做。我使用您建议的天气日志文件对我的问题进行了编辑以供参考。
-
也许你通常在你的 .bashrc 或类似文件中设置 PYTHON_PATH,这不会被 cron 自动获取?
-
@thatotherguy 谢谢你的建议。我需要找到一些相关的资源并阅读。
标签: python-3.x bash cron cron-task