简体   繁体   English

为什么 cron 作业不编译 python 文件?

[英]Why cron job doesn't compile a python file?

Cron doesn't compile my.py file and I can't get an e-mail from Cron to understand why Cron doesn't work. Cron 不编译 my.py 文件,我无法从 Cron 收到电子邮件以了解 Cron 为何不起作用。 But if I just run the command ~/t/test.sh in terminal everything works fine.但是,如果我只是在终端中运行命令~/t/test.sh一切正常。

Code in the cron: cron中的代码:

SHELL=/bin/bash
MAILTO=mymail@gmail.com
30 0-23 1-31 1-12 0-6 ~/t/test.sh

I ty to run a script test.sh which has the code:我想运行一个包含以下代码的脚本test.sh

#!/bin/bash
cd /home/alex/t && python3.8 ./test.py

Code in the test.py : test.py中的代码:

from datetime import datetime
current_time = datetime.now()
f = open("text.txt", 'w+')
f.write("Hello world! Now is {0}\n".format(current_time))
f.close()

I Launched Cron via the sudo crontab-e and crontab-e commands.我通过sudo crontab-ecrontab-e命令启动了 Cron。 I Put Cron in the first line MAILTO=mymail@gmail.com .我将 Cron 放在第一行MAILTO=mymail@gmail.com I Installed yum install mailx .我安装yum install mailx I tried adding the command >/dev/null 2>&1 to Cron, ie writing * * * * * ~/t/test.sh >/dev/null 2>&1 Nothing helps I Use Ubuntu 18.04 Python3.8.2 Here is what the logs for the grep CRON /var/log/syslog command show:我尝试将命令>/dev/null 2>&1添加到 Cron,即写* * * * * ~/t/test.sh >/dev/null 2>&1没有什么帮助我使用 Ubuntu 18.04 Python3.8.2 这是什么grep CRON /var/log/syslog命令的日志显示:

Apr 30 15:35:01 av CRON[2130]: (alex) CMD (~/t/test.sh)

Apr 30 15:35:01 av CRON[2129]: (alex) MAIL (mailer 68 bytes of output put get status 0x004b from MTA#012)

Has anyone encountered this problem?有没有人遇到过这个问题? Can you help?你能帮我吗?

your test.sh should be something like this: (needs full path)你的 test.sh 应该是这样的:(需要完整路径)

#!/bin/bash
/"location where python is installed"/python/bin/python /home/alex/t/test.py

The weakest link in you code is the fact that you invoke the Python 3.8 interpreter without a full path.您的代码中最薄弱的环节是您在没有完整路径的情况下调用 Python 3.8 解释器。 You invoke bash using a full absolute path, cd to an absolute path, and access your script through a path to the directory into which you cd-ed.您使用完整的绝对路径调用 bash,cd 到绝对路径,并通过您 cd 编辑到的目录的路径访问您的脚本。 Only Python is assuming the existence of some directories in the PATH environment variable.只有 Python 假设 PATH 环境变量中存在一些目录。

Try accessing Python 3.8 through the full path to its location.尝试通过指向其位置的完整路径访问 Python 3.8。

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

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