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
启动了 Cron和 crontab-e
命令。我把 Cron 放在第一行 MAILTO=mymail@gmail.com
.我安装了
yum install mailx
.我尝试添加命令 >/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)
有没有人遇到过这个问题?
你能帮我吗?
最佳答案
你的 test.sh 应该是这样的:(需要完整路径)
#!/bin/bash
/"location where python is installed"/python/bin/python /home/alex/t/test.py
关于python - 为什么 cron 作业不编译 python 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61525690/