Crontab无法运行python脚本(手动运行)

时间:2017-05-10 15:36:00

标签: python ubuntu cron crontab

我正处于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条目: enter image description here

1 个答案:

答案 0 :(得分:0)

要确切地知道问题是什么,请在.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。 这个日志显示了你的脚本逻辑的确切错误。