python - 无法在 crontab 中运行 python 脚本

标签 python linux ubuntu cron

我正在尝试运行 python 脚本,并且正在使用此代码来运行 crontab:

SHELL = /home/boogieman/codeCopy.py
PATH = /sbin:/bin:/usr/sbin:/usr/bin
MAILTO = ""
* * * * * /usr/bin/python/ /home/boogieman/codeCopy.py

我试过SHELL = /bin/bash也是,但仍然看不出任何区别。
当我运行 codeCopy.py在我的终端中,它就像一个魅力。但仍然无法弄清楚我在使用 crontab 时做错了什么。
这是我的一段代码,它将字典保存到 csv 文件中:
from time import strftime
t = strftime('%a, %d %b %Y %H:%M:%S')

body.update({'Time':t})

with open('/home/boogieman/r3edata.csv','a+', newline='') as fhandle:
        writer = csv.writer(fhandle)
        items = body.items()
#       writer.writerow([key for key, value in items])
        writer.writerow([value for key, value in items])


我试图在每次运行时保存字典的副本。
我在几页上找到的解决方案对我不起作用。我更改了 Shell 路径,更新了确切的文件路径,并尝试了每个用户都可以访问的脚本。
我明确使用了/usr/bin/python/在我的 crontab 命令中并尝试了 #!/usr/bin/python/在我的 python 脚本中。他们两个都没有工作。

你能帮我解决这个问题吗?先感谢您

最佳答案

/etc/crontab 中尝试以下行:

* * * * * python3 /home/boogieman/codeCopy.py

你也可以考虑这一行:
* * * * * cd /home/boogieman && python3 codeCopy.py

所以你将在正确的文件夹中运行你的脚本。

关于python - 无法在 crontab 中运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61990287/

相关文章:

python - 使用 Keras 使用 ANN 预测正弦

python - 在 keras 中的一组文档上应用共享嵌入层

linux - 更改 phpmyadmin 默认 url

ruby-on-rails - RubyMine 使用不正确/不同的 Ruby 版本 - Ubuntu

ruby - 使用包管理器时,在 Ubuntu 上哪个位置安装了 Ruby 1.9?

python - 导入错误: Could not import settings 'mysite.settings' (Is it on sys.路径?): No module named mysite.设置

类中的 Python Tkinter 滚动条

linux - 检查 Fish 中是否有重定向

linux - Raspberry Pi 临时网络

linux - 增加 ubuntu 磁盘大小的问题(virtualbox 镜像)