简体   繁体   English

Crontab Python脚本未运行

[英]Crontab Python Script not running

I know this question has been asked before but I still haven't been able to get it to work. 我知道之前曾有人问过这个问题,但我仍然无法使其正常工作。 My crontab file just has this: 我的crontab文件只有这样:

0 5 * * * /home/harry/my_env/bin/python /home/harry/compile_stats/process_tonight.py 

Here's what my process_tonight.py looks like: 这是我的process_tonight.py的样子:

import datetime
import sys
sys.path.append('/home/harry/compile_stats/')
import compile   # Module in above path

print("Processing last night\n")

date = str(datetime.datetime.today().year) + "-" + str(datetime.datetime.today().month) + "-" + str(datetime.datetime.today().day-1)

compile.process(date, date)

This file works perfectly fine when I just run it regularly from the command line but doesn't work when I schedule it. 当我仅从命令行定期运行该文件时,此文件运行良好,但在计划该文件时却不起作用。

I also looked at my /var/log/syslog file and the task I'm looking to run isn't showing up there. 我还查看了我的/ var / log / syslog文件,但我要运行的任务没有显示在此处。

Any ideas? 有任何想法吗?

EDIT: The time it's set to run in my example (5 AM) is just a random time to put in. It's not running for any time I put in there. 编辑:在我的示例中将其设置为运行的时间(上午5点)只是随机插入的时间。我在其中插入的任何时间都没有运行。

EDIT 2#: 编辑2#:

As per user speedyturkey I simplified my python script to better diagnose the problem: 根据用户speedyturkey,我简化了python脚本以更好地诊断问题:

import datetime
#import sys
#sys.path.append('/home/harry/compile_stats/')
#import compile   # Module in above path

print("Processing last night\n")

date = str(datetime.datetime.today().year) + "-" + str(datetime.datetime.today().month) + "-" + str(datetime.datetime.today().day-1)

#compile.process(date, date)

Nothing is happening so I guess the problem isn't with the import. 什么也没发生,所以我想问题不在于导入。

As per the comments, I believe the problem is in how you are calling the python script in the crontab. 根据评论,我相信问题在于您如何在crontab中调用python脚本。 Run the exact command you've given crontab and fix any problems it returns. 运行您给crontab的确切命令,并修复它返回的所有问题。

Ok, I was able to get it to work by creating a specific cron file, putting the info and there and loading it in. 好的,我可以通过创建一个特定的cron文件,将信息放到那里并加载它来使其工作。

So process_tonight.cron contains this: 因此process_tonight.cron包含以下内容:

0 5 * * * /home/harry/my_env/bin/python /home/harry/compile_stats/process_tonight.py

And I just loaded it into crontab: 然后我将其加载到crontab中:

crontab process_tonight.cron

Not really sure why this works and the other way doesn't (maybe someone else has an idea). 不太确定为什么这样做是可行的,反之则不行(也许其他人有想法)。

Instead of trying to modify the path from within your python script, you could do something like: 您可以尝试执行以下操作,而不是尝试从python脚本中修改路径:

cd /home/harry/compile_stats/ && ./process_tonight.py

which would make it easier to import compile correctly. 这样可以更容易正确地import compile Note that this would also require making process_tonight.py executable ( chmod +x process_tonight.py ) and adding a shebang pointing to your Python interpreter (I guess... #!/home/harry/my_env/bin/python). 请注意,这还需要使process_tonight.py可执行文件( chmod +x process_tonight.py )并添加指向您的Python解释器的shebang(我想...#!/ home / harry / my_env / bin / python)。

EDIT in response to Edit #2 above: It is actually not possible to tell if it is running from the code you have written - the print statements are not being redirected. 根据上面的编辑#2进行编辑:实际上,无法通过您编写的代码判断它是否正在运行-打印语句未重定向。 I suggest changing the code to perform some kind of side effect that you can check. 我建议更改代码以执行某种可以检查的副作用。 For example, import subprocess and then do (example): 例如,导入子流程然后执行(示例):

subprocess.call("date > /home/harry/compile_stats/date.txt")

If the script is executed properly, it will redirect the output of date to the file specified. 如果脚本执行正确,它将date输出重定向到指定的文件。

I know it's a bit silly, but checking your system time/timezone might be helpfull ))) I set my job to run at 5AM and when I logged in at 8AM there was no result of my script. 我知道这有点愚蠢,但是检查您的系统时间/时区可能会有所帮助。)))我将作业设置为在凌晨5点运行,当我在上午8点登录时,脚本没有结果。 So I spent over 1 hour trying to figure out what's the problem before I noticed that system time is incorrect and 5AM haven't come yet. 因此,我花了1个多小时来找出问题所在,然后才发现系统时间不正确,并且凌晨5点还没有到。

Have you tried running it from a shell script? 您是否尝试过从Shell脚本运行它? I was having the same issue with my python script. 我的python脚本遇到了同样的问题。 I ended up putting the command in a shell script and running that. 我最终将命令放入外壳脚本中并运行它。 It threw an error that the library wasn't imported, so I installed it with pip and the --user flag. 它引发了一个错误,即未导入该库,因此我使用pip和--user标志进行了安装。 Now cron runs the shell script no issues. 现在cron运行shell脚本没有问题。

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

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