python - crontab 找不到 python 模块

标签 python ubuntu cron

您好,我一直在关注 stackoverflow 帖子,但无法让 crontab 在 ubuntu 中运行我的 python 脚本。
在与下面的用户交谈后,我修改了我的文件,但没有收到“moduleNotFoundError:没有名为 aioredis 的模块”
当我停用我的 py3.9 venv 环境并调用 python feed_ingestor_to_postgres.py 时,我得到了同样的错误。很明显 pipenv 没有为我激活。有什么建议么?
我的launcher.sh 文件:

#!/bin/bash
  
WORKDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $WORKDIR

activate(){
        ../../env/bin/activate
}


python3 feed_ingestor_to_postgres.py
我的 crontab 文件:
* * * * * echo "Great cron, awesome job!" > /var/log/nonsense
* * * * * /home/ubuntu/phobos/phobos-trading-engine/exchange_feeds/launcher.sh
我不知道还能在这里做什么。任何帮助,将不胜感激

最佳答案

为了更轻松地从 crontab 管理 python 脚本,我建议使用简单的 shell 包装脚本,例如将由 crontab 调用的“launcher.sh”。启动器将 cd 到脚本位置,管理 virtualenv,或者如果需要可能设置 env vars:

#!/bin/bash

WORKDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd $WORKDIR

source ../../env/bin/activate
python feed_ingestor_to_postgres.py
然后在 crontab 中,调用启动器而不是 python 脚本:
* * * * *       ~/path/to/launcher.sh
会比直接编辑 crontab 灵活得多

关于python - crontab 找不到 python 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71663634/

相关文章:

reactjs - react JS 中的 Cronjob

python - 在 python 中解析字符串

python - sqlalchemy.exc.ProgrammingError : (psycopg2. ProgrammingError) ":"处或附近的语法错误

python - 将 html 作为字符串循环一次

ubuntu - Ansible - rsyslog 安装和配置

ubuntu - 如何在 ubuntu 中提取 .tar 文件?

ubuntu - 为什么我的 laravel 8 应用程序没有在新的 Kubuntu 20 上显示混合成功/错误通知?

kubernetes - 如何在 Kubernetes cronjob 中执行脚本 shell

python - 我们是否在双向 LSTM 中为每个批处理使用不同的权重?

cron - Ubuntu 14.04 Cron 以 ^M 结尾输出文件名