Cron and virtualenv

Posted on

Question :

Cron and virtualenv

I am trying to run a Django management command from cron. I am using virtualenv to keep my project sandboxed.

I have seen examples here and elsewhere that show running management commands from within virtualenv’s like:

0 3 * * * source /home/user/project/env/bin/activate && /home/user/project/ command arg

However, even though syslog shows an entry when the task should have started, this task never actually runs (the log file for the script is empty). If I run the line manually from the shell, it works as expected.

The only way I can currently get the command to run via cron, is to break the commands up and put them in a dumb bash wrapper script:

source /home/user/project/env/bin/activate
cd /home/user/project/
./ command arg


ars came up with a working combination of commands:

0 3 * * * cd /home/user/project && /home/user/project/env/bin/python /home/user/project/ command arg

At least in my case, invoking the activate script for the virtualenv did nothing. This works, so on with the show.

Answer #1:

You should be able to do this by using the python in your virtual environment:

/home/my/virtual/bin/python /home/my/project/ command arg

EDIT: If your django project isn’t in the PYTHONPATH, then you’ll need to switch to the right directory:

cd /home/my/project && /home/my/virtual/bin/python ...

You can also try to log the failure from cron:

cd /home/my/project && /home/my/virtual/bin/python /home/my/project/ > /tmp/cronlog.txt 2>&1

Another thing to try is to make the same change in your script at the very top:

Answered By: ars

Answer #2:

Running source from a cronfile won’t work as cron uses /bin/sh as its default shell, which doesn’t support source. You need to set the SHELL environment variable to be /bin/bash:

*/10 * * * * root source /path/to/virtualenv/bin/activate && /path/to/build/ some_command > /dev/null

It’s tricky to spot why this fails as /var/log/syslog doesn’t log the error details. Best to alias yourself to root so you get emailed with any cron errors. Simply add yourself to /etc/aliases and run sendmail -bi.

More info here:

the link above is changed to:

Answered By: DavidWinterbottom

Answer #3:

Don’t look any further:

0 3 * * * /usr/bin/env bash -c 'cd /home/user/project && source /home/user/project/env/bin/activate && ./ command arg' > /dev/null 2>&1

Generic approach:

* * * * * /usr/bin/env bash -c 'YOUR_COMMAND_HERE' > /dev/null 2>&1

The beauty about this is you DO NOT need to change the SHELL variable for crontab from sh to bash

Answered By: Basil Musa

Answer #4:

The only correct way to run python cron jobs when using a virtualenv is to activate the environment and then execute the environment’s python to run your code.

One way to do this is use virtualenv’s activate_this in your python script, see:

Another solution is echoing the complete command including activating the environment and piping it into /bin/bash. Consider this for your /etc/crontab:

***** root echo 'source /env/bin/activate; python /your/script' | /bin/bash
Answered By: Ivanhoe

Answer #5:

Rather than mucking around with virtualenv-specific shebangs, just prepend PATH onto the crontab.

From an activated virtualenv, run these three commands and python scripts should just work:

$ echo "PATH=$PATH" > myserver.cron
$ crontab -l >> myserver.cron
$ crontab myserver.cron

The crontab’s first line should now look like this:

PATH=/home/me/virtualenv/bin:/usr/bin:/bin:  # [etc...]
Answered By: joemaller

Answer #6:

The best solution for me was to both

  • use the python binary in the venv bin/ directory
  • set the python path
    to include the venv modules directory.

man python mentions modifying the path in shell at $PYTHONPATH or in python with sys.path

Other answers mention ideas for doing this using the shell. From python, adding the following lines to my script allows me to successfully run it directly from cron.

import sys

Here’s how it looks in an interactive session —

Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> sys.path
['', '/usr/lib/python3.3', '/usr/lib/python3.3/plat-x86_64-linux-gnu', '/usr/lib/python3.3/lib-dynload']

>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'   

>>> sys.path.insert(0,'/path/to/venv/modules/');

>>> import requests
Answered By: here

Answer #7:

I’d like to add this because I spent some time solving the issue and did not find an answer here for combination of variables usage in cron and virtualenv. So maybe it’ll help someone.

DIR_SMTH="cd /smth"
VENV=". venv/bin/activate"
CMD="some_python_bin do_something"
# m h  dom mon dow   command
0 * * * * $DIR_SMTH && $VENV && $CMD -k2 some_target >> /tmp/crontest.log 2>&1

It did not work well when it was configured like

DIR_SMTH=”cd /smth && . venv/bin/activate”

Thanks @davidwinterbottom, @reed-sandberg and @mkb for giving the right direction. The accepted answer actually works fine until your python need to run a script which have to run another python binary from venv/bin directory.

Answered By: Dmitriy

Answer #8:

This is a solution that has worked well for me.

source /root/miniconda3/etc/profile.d/ && 
conda activate <your_env> && 
python <your_application> &

I am using miniconda with Conda version 4.7.12 on a Ubuntu 18.04.3 LTS.

I am able to place the above inside a script and run it via crontab as well without any trouble.

Leave a Reply

Your email address will not be published. Required fields are marked *