简体   繁体   English

`/bin/sh: 1: python: not found` 当通过 docker 中的 cron 运行时

[英]`/bin/sh: 1: python: not found` when run via cron in docker

I want to repeatedly call a script via cron in a docker container, but when I switch from one time execution to execution via cron the official python image suddenly can't seem to find python.我想在 docker 容器中通过 cron 反复调用脚本,但是当我从一次执行切换到通过 cron 执行时,官方 python 图像突然似乎找不到 python。

Dockerfile: Dockerfile:

FROM python:3.7-slim

COPY main.py /home/main.py

#A: works
CMD [ "python", "/home/main.py" ]

#B: doesn't work
#RUN  apt-get update && apt-get -y install -qq --force-yes cron
#COPY hello-cron /etc/cron.d/hello-cron
#CMD ["cron", "-f"]

main.py主文件

import time

for i in range(90000):
    print(i)
    time.sleep(5000)

hello-cron:你好,cron:

* * * * * root python /home/main.py > /proc/1/fd/1 2> /proc/1/fd/2
#

When I switch A for B in the Dockerfile the error message is: /bin/sh: 1: python: not found当我在 Dockerfile 中将 A 切换为 B 时,错误消息是: /bin/sh: 1: python: not found

Thank you all for he quick responses!谢谢大家的快速回复! Adding PATH=/usr/local/bin in the cron file solved my problem.在 cron 文件中添加PATH=/usr/local/bin解决了我的问题。

Cron doesn't set up the PATH environment variable the same as a normal login shell so python can't be found. Cron 没有像正常登录一样设置PATH环境变量 shell 所以python It should work if you specify a complete path to the Python executable, eg replace python with /usr/bin/python (or whatever the path to your Python executable happens to be).如果您指定 Python 可执行文件的完整路径,它应该可以工作,例如用/usr/bin/python替换python (或您的 Python 可执行文件的路径碰巧)。 Alternatively you can explicitly set the PATH environment variable in the Cron configuration file to include the directory where Python can be found.或者,您可以在 Cron 配置文件中显式设置PATH环境变量,以包含可以找到 Python 的目录。

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

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