Search code examples
pythonbashcronpipenvpyenv

How schedule crontab job when using pyenv and pipenv


I'm on Ubuntu 20.04, and I am having issues setting up the crontab correctly using pyenv + pipenv together. Simply adding pipenv run python script.py to the cronjob does not work; I think it may be due to:

  1. the environment required by pyenv
  2. the non-interactive shell of cronjob

UPDATED BIG QUESTION

How do I use /home/jennings/.pyenv/shims/pipenv correctly in crontab??

I've checked $? for pipenv -v in run.sh scheduled in crontab, and it fails.

https://github.com/pyenv/pyenv#advanced-configuration

00. (for reference) Bash startup

pyenv requires these entries in these startup files for interactive/login shells I don't understand how to translate this to a non-interactive cronjob call. How do I set up my BASH_ENV to emmulate these environents below? https://stackoverflow.com/a/9954208/9335288

# ~/.profile
eval "$(pyenv init --path)"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
fi
# ~/.bashrc:
export PYENV_ROOT="$HOME/.pyenv"

I. Crontab

I am trying to effectively write a .profile for the cronjob... but if you a better solution, please let me know.

# CRONTAB

SHELL=/bin/bash
BASH_ENV="home/jennings/.custom_bash_env
# BASH_ENV="home/jennings/.profile"
# BASH_ENV="home/jennings/.bashrc"
* * * * * cd $PROJECT_DIR; ./run.sh

# BASH_ENV:

Should I point to .profile or .bashrc instead?*

# PYENV
#eval "$(pyenv init --path)"
#if command -v pyenv 1>/dev/null 2>&1; then
#  eval "$(pyenv init --path)"
#fi

# ENV VARIABLES
PYENV_ROOT="/home/jennings/.pyenv"
PYTHONPATH=$SOMEPLACE
PATH=all:of:them:paths

II. Project Folder

# run.sh:

#!/usr/bin/bash
# PYENV
eval "$(pyenv init --path)"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
fi
# actual pipenv command I'm trying to run
pipenv run python main.py

# main.py:

import this
# Does some python and logging

What I've tried and know

  • Pointing directly at the .profile and .bashrc doesn't work
  • run.sh will run okay; it's thepipenv step that fails
  • Scheduling other non-pipenv commands work fine

pyenv init block

  • Placed in the BASH_ENV file, the cronjob doesn't run at all
  • Placed in the run.sh, the cronjob now runs, but the pipenv run still fails

pipenv related

  • I've tried pipenv shell; which python to use that one in the crontab enjoy -- no cigar

Solution

  • Cron has a limited environment. You have to declare the full path to the pipenv executable. Instead of using pipenv run python script.py you should use /usr/local/bin/pipenv run python script.py.

    In your case the full path can be different which pipenv will give you the proper path.

    This is explained in this post how to run pipenv in cronjob in ubuntu?