简体   繁体   English

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

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

I am in the learning phase of ubuntu server.我在ubuntu服务器的学习阶段。 I have a python script that texts me the weather each morning, that script works perfectly when I run it from the command line.我有一个 python 脚本,每天早上都会给我发送天气信息,当我从命令行运行它时,该脚本运行良好。 However when I try to set up a crontab entry, it does not work.但是,当我尝试设置 crontab 条目时,它不起作用。 The following line is what I put into my crontab file:以下行是我放入 crontab 文件的内容:

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

I also tried the following:我还尝试了以下方法:

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

When I type either of the following commands into the terminal, my python script works great.当我在终端中键入以下任一命令时,我的 python 脚本运行良好。

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

It has to be a problem with cron, right?应该是cron的问题吧? Thanks for your time if you are viewing this now.如果您现在正在查看此内容,感谢您的时间。

Edit: Here is the system log cron entries:编辑:这是系统日志 cron 条目: 在此处输入图片说明

To know what is the problem exactly, write your commands in a .sh要确切了解问题所在,请将您的命令写入 .sh

In you case:在你的情况下:

myscript.sh脚本文件

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

With set -x all commands are printed in console.使用set -x所有命令都打印在控制台中。

and then execute your script with cron:然后用 cron 执行你的脚本:

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

where /anypath/cronlog.log will be created automatically.其中/anypath/cronlog.log将自动创建。 this log show you what is the exactly error of your logic of script.此日志向您显示脚本逻辑的确切错误是什么。

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

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