您好,我一直在关注 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/