繁体   English   中英

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

[英]Crontab failing to run a python script (runs fine manually)

我在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 条目: 在此处输入图片说明

要确切了解问题所在,请将您的命令写入 .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将自动创建。 此日志向您显示脚本逻辑的确切错误是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM