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.
Related
I am using a virtualbox with ubuntu 20.04. Within the VM I have a condo environment and installed python-crontab using:
conda install -c conda-forge python-crontab
I want to continuously run a certain python file which will write to a database. I can make this happen when using job.run() but not when the job is actually on a time schedule.
I have tried the following things:
crontab -e
then adding in
* * * * * python /home/cron/... to my python file
I have also tried:
sudo crontab -e
then adding in
* * * * * python /home/cron/... to my python file
as well as just making a call to python and running the following:
from crontab import CronTab
cron = CronTab(user=True)
job = cron.new(command='python /home/cron/... to my python file')
job.minute.every(1)
cron.write()
user=True I have also tried replacing by user=cron
Now as mentioned I can run job.run() and have new entries in my database. But when it is on a schedule nothing gets written to my database anymore.
My syslog file says the following:
(cron) CMD (python /home/cron/... to my python file)
(CRON) info (No MTA installed, discarding output)
(root) CDM (python /home/cron/... to my python file)
These messages do get written every minute, so cron does seem to run, why do I not see any output in my database then?
ps with some digging on the internet, I was told that the info message shouldn't interfere with the cron job itself
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
I currently have a python script at
/home/ubuntu/test/test.py
When this script runs, it writes to a file
/home/ubuntu/test/test.txt
I am completely new to cron, and not very familiar with linux in general. I am trying to set up a cronjob that basically runs this script every minute.
I saw some people suggest #!/usr/bin/env python so I added it, but I noticed I don't even have a env folder in /usr/bin
I then ran chmod -x test.py. Then added an entry to cron * * * * * /home/ubuntu/test/test.py. Noted this wasn't working and saw someone suggest trying * * * * * /home/ubuntu/test/test.py 2>&1 /tmp/testlog.log. But when I check /tmp i only see a folder crontab.8Rxowt/crontab/cron and i don't see any log file created.
I am kind of confused now, I can't figure out why nothing is being updated at all. I'm not sure if the script being run needs to be placed somewhere specific, or if I screwed something up with my cron installation, or something else altogether.
I noticed trying to run ./test.py gives permission denied, and sudo ./test.py gives command not found. Is my shebang not working? I verified im using unix line endings.
To make it run every minute you have to add the path to python from your system:
* * * * * /usr/bin/python cd /path_to/test.py
I suggest you to test it with a simple command such as "touch"
* * * * * /usr/bin/touch cd /path_to/test.txt
https://crontab.guru/every-1-minute
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 am trying to run a python file that takes in one argument (CSV file). It works when i run the script in the terminal but it gives errors when i run it in cron.
This is the line that i run in the terminal:
python nb2.py my_csv_file.csv
And here is my code that i am trying to run in cron:
42 13 * * * /usr/local/bin/python2.7 ~/nb/Development/code/nb2.py ~/nb/Development/code/my_csv_file.csv &> /tmp/June_QB_cat.log
The error says that it cannot find a sqlite table file which is already in the code folder.
Note that when you run the command in the terminal, you're in the ~/nb/Development/code directory already and so your current working directory is that; when you run in it cron, it is not. I would suggest either doing (in your cron job) cd ~/nb/Development/code && python nb2.py my_csv_file.csv &> /tmp/logfile.txt or doing an os.chdir("~/nb/Development/code") as the first step in your code. (Also, I'd suggest doing /home/username instead of ~ just in case you aren't running cronjob as your username at some point, but given the error you get, that sounds like it's not an issue)
You can get the path of a file relative to the current script with
import os.path
relative_path = os.path.join( os.path.dirname(__file__), "sqlitetable" )