9

pyenv 下 crontab 执行不了 python 脚本_crontab 未执行脚本 - 文渊博客

 3 years ago
source link: https://www.wenyuanblog.com/blogs/python-crontab-script-not-work-in-pyenv.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

crontab 定时执行 pyenv 下的 python 脚本不成功

一、问题描述

通过 pyenv 管理不同版本的 Python,但使用 crontab 定时执行脚本时,脚本未成功运行。

二、问题原因

crontab 中需要设置启动该 Python 脚本的解释器路径。

三、解决示例

假设我有个脚本 check_disk.py/home/devops/ 用户目录下,且 pyenv 也是安装在该用户目录下的,那么 crontab 中应该这样编辑:

# 每小时的50分执行一次脚本
50 */1 * * * /home/devops/.pyenv/shims/python /home/devops/scripts/check_disk.py >/dev/null 2>&1

如果要打日志输出的话:

# 每小时的50分执行一次脚本
# 日志会打印输出到 /home/devops/scripts/logs/check_disk.log
50 */1 * * * /home/devops/.pyenv/shims/python /home/devops/scripts/check_disk.py >/home/devops/scripts/logs/check_disk.log

补充:如何查看当前 Python 解释器路径

执行如下命令:

which python

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK