Search code examples
pythondjangocron

Can't run crontab django command


I have a bash script run.sh --- -rwxrwxr-x (I did chmod +x run.sh)

When i run ./run.sh script works fine (appended data to the file)

But It did not work in crontab

crontab -l shows * * * * * ./run.sh

#!/bin/bash

export DJANGO_SETTINGS_MODULE=eda_parser.settings # eda_parser -- name of the project and the main app
cd /home/alex/root_folder/projects/5_eda_parser/eda_parser
source ../venv/bin/activate # run venv
python manage.py delete_old # main django command

eda_parser/
├── manage.py
├── eda_parser # main app
...
├── run.sh # script to run
├── scraper # django commands app
│   ...
│   ├── management
│   │   └── commands
│   │       ├── delete_old.py # code of the command

Solution

    1. activate your virtual environment
    2. type in which python to know the path of your python executable and then use this python executable path in your shell script.
    #!/bin/bash
    export DJANGO_SETTINGS_MODULE=eda_parser.settings # eda_parser -- name of the project and the main app
    /home/sysadmin/.virtualenvs/virtual_environment/bin/python /absolute/path/of/your/manage.py delete_old # main django command
    

    also, you need to give the absolute path of your shell script when setting up crontab. let's suppose you want it to run at 01:15, append this in crontab -e.

    15 1 * * * /absolute/path/to/your/shell/script.sh