Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么cron job没有';你不能编译python文件吗?_Python_Linux_Ubuntu_Cron - Fatal编程技术网

为什么cron job没有';你不能编译python文件吗?

为什么cron job没有';你不能编译python文件吗?,python,linux,ubuntu,cron,Python,Linux,Ubuntu,Cron,Cron没有编译我的.py文件,我也无法从Cron那里得到一封电子邮件来理解Cron为什么不能工作。但是如果我只是在终端中运行命令~/t/test.sh,一切正常 cron中的代码: SHELL=/bin/bash MAILTO=mymail@gmail.com 30 0-23 1-31 1-12 0-6 ~/t/test.sh 可以运行脚本test.sh,该脚本包含以下代码: #!/bin/bash cd /home/alex/t && python3.8 ./test.p

Cron没有编译我的.py文件,我也无法从Cron那里得到一封电子邮件来理解Cron为什么不能工作。但是如果我只是在终端中运行命令
~/t/test.sh
,一切正常

cron中的代码:

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

可以运行脚本
test.sh
,该脚本包含以下代码:

#!/bin/bash
cd /home/alex/t && python3.8 ./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()
我通过
sudo crontab-e
crontab-e
命令启动了Cron。我把Cron放在MAILTO的第一行
=mymail@gmail.com
。 我安装了
yum安装mailx
。我尝试将命令
/dev/null 2>&1
添加到Cron,即写入
***~/t/test.sh>/dev/null 2>&1
无济于事 我使用Ubuntu 18.04 蟒蛇3.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)邮件(邮件输出68字节,从MTA获取状态0x004b#012)

有人遇到过这个问题吗?
您能帮忙吗?

代码中最薄弱的一个环节是调用Python 3.8解释器时没有完整的路径。您可以使用完全绝对路径调用bash,将cd转换为绝对路径,并通过cd-ed目录的路径访问脚本。只有Python假设path环境变量中存在一些目录


尝试通过到达其位置的完整路径访问Python 3.8。

您的test.sh应该是这样的:(需要完整路径)

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