Crontab无法在Mac OS上运行python [英] Crontab won't run python on Mac OS

查看:110
本文介绍了Crontab无法在Mac OS上运行python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了所有可以找到的答案,并尝试了所有方法,但是我仍然无法让crontab运行python.我什至创建了一个shell脚本来运行python,以为隔离会有所帮助,但仍然行不通.

I have chased down every answer I can find and tried everything, but I still can't get my crontab to run python. I even created a shell script to run the python, thinking that isolation would help, but it still doesn't work.

我在crontab中有以下条目

I have the following entries in crontab

* * * * * echo "hello" >> /tmp/test2.txt
* * * * * work.sh

第一行每分钟运行一次,以便我可以验证cron是否正在运行.我已验证它正在运行.每分钟我在test2.txt中看到一个新的"hello"

the first line just runs every minute so that I can verify cron is running. I have verified that it is running. Every minute I see a new "hello" in test2.txt

第二行运行以下名为work.sh的脚本

the second line runs the following script named work.sh

#! /usr/bin/env zsh
echo "Hello World!" >> /tmp/test2.txt
/opt/miniconda3/bin/python3.7 hellopy.py

hellopy.py然后将来自Python的Hello"写入test2.txt,如下所示:

hellopy.py then writes "Hello from Python" into test2.txt, like so:

with open('/tmp/test2.txt','a') as test_txt:
        test_txt.write("Hello from Python")

当我手动运行work.sh时,我在test2.txt中得到了期望的结果:

when I run work.sh manually I get what I expect in test2.txt:

Hello World!
Hello from Python

但是当我让cron运行一段时间并查看test.txt时,我所看到的只是从直接cron行输入的内容,然后是来自work.sh的echo命令.python行未写入文件.文本"Hello from Python"没有显示.

But when I let cron run for a while and look at test.txt, all I see is the entry from from the direct cron line and then the echo command from work.sh. The python line does not write to the file. The text "Hello from Python" does not show up.

hello
Hello World!
hello
Hello World!
hello
Hello World!

我已经检查了python文件 -rwxr-xr-x 和文本文件 -rwxrwxrwx 的权限,它们看起来不错.

I have checked the permissions of the python file -rwxr-xr-x and the text file -rwxrwxrwx and they look fine.

所以我知道

  1. python代码有效
  2. shell脚本可以成功运行python代码
  3. crontab正在运行
  4. crontab可以运行Shell脚本
  5. shell脚本的某些部分在由crontab运行时可以正确执行

但是由于某些原因,当crontab运行shell脚本时,python命令不起作用.我也尝试了以下方法,但都无济于事.

But for some reason, when crontab runs the shell script, the python command doesn't work. I have also tried the following, none of which work.

  1. 直接从crontab中的一行而不是通过shell脚本运行python脚本.
  2. 在shebang中使用"bash"代替"zsh"
  3. 在不同的路径/usr/bin/python3中使用不同的python副本

我在所有搜索中一直遇到的解决方案与文件许可权和绝对路径有关.我尝试了所有可能的组合,但似乎无济于事.我完全被困住了.任何帮助将不胜感激.

The solutions that I kept coming across in all my searching have to do with file permissions and absolute paths. I have tried every possible combination of that but nothing seems to help. I am completely stuck. Any help would be appreciated.

推荐答案

此处从@chepner捕获解决方案以供后代使用:

Capturing the solution from @chepner here for posterity:

问题是cron希望在我的主目录中找到我的python文件,但是我将python文件保存在另一个文件中.将文件移到主目录后,一切正常.

The issue was that cron was expecting to find my python file in my home directory, but I had saved the python file in a different one. Once I moved the file to the home directory, everything worked fine.

这篇关于Crontab无法在Mac OS上运行python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆