【问题标题】:Crontab failing to run a python script (runs fine manually)Crontab 无法运行 python 脚本(手动运行良好)
【发布时间】:2020-08-31 17:58:04
【问题描述】:

我正处于 ubuntu 服务器的学习阶段。我有一个 Python 脚本,每天早上都会给我发送天气信息,当我从命令行运行它时,该脚本可以完美运行。但是,当我尝试设置 crontab 条目时,它不起作用。以下行是我放入 crontab 文件的内容:

30 11 * * * cd /home/ubuntu/documents && /usr/bin/python /home/ubuntu/documents/weatherText.py

我还尝试了以下方法:

30 11 * * * /usr/bin/python /home/ubuntu/documents/weatherText.py

当我在终端中输入以下任一命令时,我的 python 脚本运行良好。

1.) cd /home/ubuntu/documents && /usr/bin/python /home/ubuntu/documents/weatherText.py
2.) /usr/bin/python /home/ubuntu/documents/weatherText.py

这一定是cron的问题,对吧?如果您现在正在查看此内容,感谢您抽出宝贵时间。

编辑:这是系统日志 cron 条目:

【问题讨论】:

  • 你检查 cron 的日志文件了吗?
  • @MattSchuchard 我已将它们添加到我的帖子中!

标签: python ubuntu cron crontab


【解决方案1】:

要确切知道问题出在哪里,请将您的命令写在 .sh 中

在你的情况下:

myscript.sh

#!/bin/bash
set -x
cd /home/ubuntu/documents
/usr/bin/python /home/ubuntu/documents/weatherText.py

使用set -x,所有命令都打印在控制台中。

然后用 cron 执行你的脚本:

30 11 * * * /fullpathscript/myscript.sh > /anypath/cronlog.log 2>&1

/anypath/cronlog.log 将被自动创建。 此日志向您显示脚本逻辑的确切错误。

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 2014-01-02
    • 1970-01-01
    • 2016-12-18
    • 2016-03-06
    • 2020-09-09
    • 2023-02-22
    • 2021-05-30
    相关资源
    最近更新 更多