Reputation: 271
I am using csvimporter to import some a csv file into a Django model. I have 2 scripts - one python script to take the file:
import subprocess
subprocess.call("python manage.py csvimport --model='csv_reader.csv' /Users/path_to_csv", shell = True)
And a django script to delete objects from the model:
from csv_reader.models import *
csv.objects.all().delete()
Both of the scripts work fine when ran manually from the shell. But when I add a cron job to perform the execution of the scripts, it's not working, although it logs them in cron log:
Feb 25 10:21:00 Liubous-MacBook-Pro.local /usr/sbin/cron[43055]: (yudasinal1) CMD (/Users/path_to_script)
I tried adding a cronjob like this:
DJANGO_SETTINGS_MODULE=project.settings
* * * * * /Users/path_to_csv/test_subprocess.py
Where in the actual script I added #!/usr/bin/env python
at the top of the file.
As well as I tried adding this cronjob:
DJANGO_SETTINGS_MODULE=project.settings
* * * * * python /Users/path_to_csv/test_subprocess.py
All of them are logged into cron log, but unfortunately, the actual functions are not being executed.
Any help would be appreciated!
Upvotes: 0
Views: 985
Reputation: 136339
Unix scripts use a line called "Shebang"
So your first line should look like this:
#!/usr/bin/env python
mysript.py
chmod +x myscript.py
in console../myscript.py
.crontab -e
in terminal.Add a line like this:
30 13 * * * /home/yourusername/myscript.py
Verify with crontab -l
that everything worked.
(see cyberciti.biz for more information)
import datetime
import getpass
now = datetime.datetime.now()
# Open file to append
with open("/home/user/myscript.log", "a") as f:
f.write("Script started at %i.%i.%i (%i:%i:%i) by %s" % (now.day, now.month, now.year, now.hour, now.minute, now.second, getpass.getuser()))
[...]
with open("/home/user/myscript.log", "a") as f:
f.write("File 'xy' was opened.")
Upvotes: 1
Reputation: 1270
In your terminal write crotab -e . There put your
* * * * * /usr/bin/python /Users/path_to_csv/test_subprocess.py
And in the test_subprocess.py add
DJANGO_SETTINGS_MODULE=project.settings
and also note that DJANGO_SETTINGS_MODULE=project.settings will only work if you run this cron job in project folder. So it would be better to use it as DJANGO_SETTINGS_MODULE=/pathToProject/project.settings
Upvotes: 0
Reputation: 184
just set project setting full path to DJANGO_SETTINGS_MODULE
DJANGO_SETTINGS_MODULE=/Users/path_to_project/project.settings
Upvotes: 0
Reputation: 387
check first your crontab is running. add a job to create a file in tmp folder. IF it is running. try to give full path for python like
* /usr/bin/python /home/path of your django project/manage.py ...
Upvotes: 0