pythondjangopythonanywhere

Issues with Running Django Management Command as an Always On Task on PythonAnywhere


I built and deployed my Django website on PythonAnywhere.

I have a script in my

app/management/commands/my_command.py

that I intend to keep always running. I tried using the PythonAnywhere Always On Task feature for this, but the status never changes from "starting" to "running".

There is no problem with the script itself because when I run:

bash
(venv) $ source /home/myusername/.virtualenvs/myenv/bin/activate && cd /home/myusername/myproject && python manage.py my_command

the script runs successfully, and then when I go to my Always On Task console, I see that it ran successfully and was recorded in that console.

But the problem is that I don't want to always manually run it in my Bash console.

I have followed the instructions on the PythonAnywhere help page, and I have changed my task command to all the options I can use. I tried:

bash
source virtualenvwrapper.sh && workon myenv && python /home/myusername/myproject/manage.py my_command

But it never changes to "running."

I also tried:

bash
/home/myusername/.virtualenvs/myenv/bin/python /home/myusername/myproject/manage.py my_command

Again, it never changes to "running."

However, if I run the script in the Bash console, the Always On task prints out my success message!

What am I doing wrong?


Solution

  • I resolved the issue by specifying the DJANGO_SETTINGS_MODULE in the command. The correct command for the Always On Task is:

    DJANGO_SETTINGS_MODULE=myproject.settings /home/myusername/.virtualenvs/myenv/bin/python /home/myusername/myproject/manage.py my_command
    

    This allowed the task to start successfully and run continuously as intended.