Tuesday, February 1, 2022

[SOLVED] schedule django custom commands with cron inside a virtualenvironment

Issue

I have written a custom management command post_message when I run python manage.py post_message the command executes well.

Now I want this command to run every 10 minutes.

I have a virtual envrironment.

I have a file - msg.cron the contents of the file are as follows -

#!SHELL=/bin/bash
*/10 * * * * source /home/username/Envs/project_name/bin/activate && /home/username/Code/project_name/manage.py post_message > /dev/null

I have done chmod +x on msg.cron

Having done this, I added

crontab msg.cron

Now when I do crontab -l contents of msg.cron are shown.

But the management command are not being run, what am I missing?


Solution

You don't need to activate the virtualenv in this case. You can just use the python in the bin directory of the virtualenv.

*/10 * * * * source /home/username/Envs/project_name/bin/python /home/username/Code/project_name/manage.py post_message > /dev/null


Answered By - schillingt
Answer Checked By - Gilberto Lyons (WPSolving Admin)