I have an app in a django website that looks like in the tutorial of this page django_documentation and it runs very well if I run the command:
python manage.py closepoll.py
After I installed Crontab and I put it in INSTALLED APPS in settings.py file. Also I put this line in my settings.py file:
CRONJOBS = [
('*/5 * * * *', 'django.core.management.call_command', ['closepoll']),
]
To be able to run the script every 5 minutes. I add the cronjob through:
python manage.py crontab add
And it was added perfectly. After I restart the server but nothing is happening every 5 minutes. Do you know what I'm doing wrong?
Related
I have installed django-crontab to the python virtual environment. I am attempting to add a row to a Google sheet daily, I know the function works so I just need a cron job to trigger it.
python 3.6.7
django 2.0.9
django-crontab 7.0.1
Below is my folder structure:
├── api
│ ├── utils
│ | └── cron.py
|---manage.py
Here is my cron.py
# For Google API to sheets
import pygsheets
# The ID of spreadsheet.
SPREADSHEET_ID = 'abc123'
def addToGraphs():
# Use service credentials
print('Attempt CRON')
gs = pygsheets.authorize(service_account_file='credentials.json')
spreadsheet = gs.open_by_key(SPREADSHEET_ID)
graphsWorksheet = spreadsheet.worksheet_by_title('Graphs')
graphsWorksheet.append_table(["29/12/2020", 1, 2, 3])
return True
In my settings.py I have added CRONJOBS like so:
CRONJOBS = [
('*/1 * * * *', 'utils.cron.addToGraphs')
]
Using the commands that the documentation says, I am seemingly correctly adding the cron using:
python manage.py crontab add .
And then able to list that correctly using:
python manage.py crontab show
When I run it locally using:
python manage.py crontab run {taskId}
The function does print out "Attempt CRON" and adds the row to the Google sheet, yay! But only once... It then just ends.
When I kick off the python django server with:
python manage.py runserver
I get no print statement and the Google doc doesn't update.
What am I missing? I feel I am close as when I run it manually it works. Any advice is appreciated, thank you!
Relative paths didn't work I had to update the path to my credentials.json file to an absolute path.
I changed:
credentails.json
to
/Users/Matt/Documents/Project/Backend/api/credentials.json
I wish to ping_google for the sitemap every 10 minutes using crontab and django manage.py
I need to run the following on server-
python manage.py ping_google /sitemap.xml
I am not sure how to give the parameter '/sitemap.xml' in the crontab. Here is what I have
*/10 * * * * /home/project/.virtualenvs/app/bin/python /home/project/workspace/app-backend/django-app/manage.py ping_google /sitemap.xml
Am I passing the argument correctly? How am I supposed to pass the argument?
I think it might be better here to write a small shell script here that sets the current working directory correctly, and activates the virtual environment. For example a file command.sh in the directory /home/project/workspace/app-backend/django-app/:
#!/bin/bash
# cd to the correct directory
cd "/home/project/workspace/app-backend/django-app/"
# source the virtual environment
. /home/project/.virtualenvs/app/bin/activate
# call the command
python manage.py ping_google /sitemap.xml
Then you can run this with:
*/10 * * * * bash /home/project/workspace/app-backend/django-app/command.sh
This is my fabric script that runs on the jenkins server.
sudo('/home/myjenkins/killit.sh',pty=False)
sudo('/home/myjenkins/makedir.sh',pty=False)
sudo('/home/myjenkins/runit.sh',pty=False)
This kills the old server, creates a virtualenv, installs the requirements and restarts the server.
The problem is the with the script that starts the server - runit.sh :-
nohup /home/myjenkins/devserver/dev/bin/python /home/myjenkins/devserver
/workspace/manage.py runserver --noreload 0:80 >> jenkins.out &
When the jenkins server that starts the server and I navigate to the homepage, it gives me a 404 Page Not Found. It says /static/index.html not found. But the file exists. When I run 'sudo bash runit.sh' and I access the homepage, It works fine.
mkdir -p /home/myjenkins/devserver
cp -rf /home/myjenkins/workspace /home/jenkins/devserver/
cp -f /home/myjenkins/dev_settings.py /home/myjenkins/devserver/workspace/mywebsite/settings.py
cd /home/myjenkins/devserver
virtualenv -p python3 dev
cd /home/myjenkins/devserver/workspace
../dev/bin/pip install -r requirements.txt
Please ask me for more details if you need it.
EDITED 9/2/18
When I start the script from the folder containing manage.py, the server is able to serve the files. But Jenkins was starting the script from the home folder and if I also start the script from the home folder - the server is not able to find the files. look at my comment for more details. It would be great if someone could explain why this happens even though I've specified the full path in the script.
nohup /home/myjenkins/devserver/dev/bin/python /home/myjenkins/devserver
/workspace/manage.py runserver --noreload 0:80 >> jenkins.out &
Okay I figured out the whole deal. My django server was taking the output of the npm build from the wrong folder.
In the settings.py file, the variable STATICFILES_DIRS was set as:-
STATICFILES_DIRS = ('frontend/dist',)
instead of:-
STATICFILES_DIRS = (os.path.join(BASE_DIR,'frontend/dist'),)
Thus, when Jenkins was running the script, it was doing so from the home folder. This made Django's staticfiles finders to look at /home/myjenkins/frontend/dist instead of the relative '../frontend/dist'
I still pretty new to the use of crontab in Django. I have followed the instruction in this link https://hprog99.wordpress.com/2014/08/14/how-to-setup-django-cron-jobs/ to help me set up my method, my_scheduled_job() in cron.py file, which I want to call every five minutes.
Here is my updated setting.py
INSTALLED_APPS = (
'django_crontab',
...)
CRONJOBS = [
('*/5 * * * *', 'myproject.cron.my_scheduled_job')
]
After which I ran this: python manage.py crontab add
Output: adding cronjob: (d0428c9ae8227ed78b17bf32aca4bc67) -> ('*/5 * * * *', 'cinemas.cron.my_scheduled_job')
Next: Nothing happens.
How do I start this cron job locally? Is there a way to test if my job ran normally?
In django you can setup cron using django-chronograph or chronograph.
Django-chronograph is a simple application that allows you to control the frequency at which a Django management command gets run.
Step 1:
Create management command of your task in django. For creating django management command refer Writing custom django-admin commands.
Step 2:
After creating django management command configure command in cronograph.
Hope this helps you.
first, you must Specify your project profile, then add the cron job, if your project name is foo, then it is like:
export FOO_PROFILE = 'local'
python manage.py crontab add
and , before run above command, in your settings.local you should config cron environment first, something like:
# django-crontab config
CRONTAB_DJANGO_SETTINGS_MODULE = 'gold.settings.local'
Is there a test tab in Python? Probably a simulation somewhere to get tutorial. Try key word search in agent answer or similar help and command program.
python manage.py runserver
Once you start the server, crontab will automatically execute based on the time specified in settings.py.
I have written a script in django that send queued emails saved in the database to the users. There is a mangement command which should be called by crontab every hour to send the emails. However, whenever the crontab job get executed I receive the following error:
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: libpq.so.5: cannot open shared object file: No such file or directory
My django app works fine without any error. But running crontab fails. The following is my crontab config:
0 * * * * source /opt/portal/virtEnv/bin/activate && python /opt/portal/websource/manage.py send_queued_messages --limit=1
Does anybody know how to solve the problem?
AFAIK you cant use the activate script in cronjob, just point it to the python interpreter which is in your virtualenv and it will work:
0 * * * * /opt/portal/virtEnv/bin/python /opt/portal/websource/manage.py send_queued_messages --limit=1
I found the solution.
I had to add LD_LIBRARY_PATH environment variable to the crontab configuration with the path pointing to the lib folder of postgres db.
Now it works smoothly.
try to point to python version inside virtualenv. Do not activate and use python.
Try this:
0 * * * * /opt/portal/virtEnv/bin/python /opt/portal/websource/manage.py send_queued_messages --limit=1
Edited: Removed 'source' at the beginning of the command.