Reputation: 456
I'm developing django-python project and stuck in doing django-crontab for automate file backup, this is the code:
In my settings.py
INSTALLED_APPS = (
.
.
.
'django-crontab',
)
CRONTAB_DJANGO_MANAGE_PATH = '/home/path/to/my/project/manage.py'
CRONJOBS = [
('25 15 * * *', 'cd /home/path/to/my/project && /usr/bin/sh myscript.sh'),
]
In myscript.sh
#!/bin/sh
set -e
cd /home/path/to/my/project/
exec cp file some_dir/copy_file
I test this code on my server to run at 15:25 o'clock and it's not executing the myscript.sh
The script is just doing copy file to another directory (for back up) and I've tested manually using ./myscript.sh in server, the script is working well...
I've been trying various methodology, but nothing seems works
I take the reference from django-crontab (https://pypi.python.org/pypi/django-crontab/0.5.1) and this site (https://superuser.com/questions/81262/how-to-execute-shell-script-via-crontab) + http://github.com/kraiz/django-crontab
EDIT
I've add my cronjob using this command as well
python manage.py crontab add
I've checked the job is there using this command
python manage.py crontab show
WORKING EXAMPLE
settings.py
CRONJOBS = [
# Auto Backup Database everyday at 4 am
('0 4 * * *', 'django.core.management.call_command', ['dumpdata', 'myproject'], {'indent': 4, 'use_natural_keys': True}, '> /home/mydirectory/myproject_backup/myproject_backup.json'),
]
myproject_backup.json
[
{
"pk": 1,
"model": "contenttypes.contenttype",
"fields": {
"model": "permission",
"name": "permission",
"app_label": "auth"
}
},
{
"pk": 2,
"model": "contenttypes.contenttype",
"fields": {
"model": "group",
"name": "group",
"app_label": "auth"
}
},
{
"pk": 3,
"model": "contenttypes.contenttype",
"fields": {
"model": "user",
"name": "user",
"app_label": "auth"
}
},
{
"pk": 4,
"model": "contenttypes.contenttype",
"fields": {
"model": "contenttype",
"name": "content type",
"app_label": "contenttypes"
}
},
{
"pk": 5,
"model": "contenttypes.contenttype",
"fields": {
"model": "session",
"name": "session",
"app_label": "sessions"
}
},
{
"pk": 6,
"model": "contenttypes.contenttype",
"fields": {
"model": "site",
"name": "site",
"app_label": "sites"
}
},
{
"pk": 7,
"model": "contenttypes.contenttype",
"fields": {
"model": "logentry",
"name": "log entry",
"app_label": "admin"
}
},
{
"pk": 8,
"model": "contenttypes.contenttype",
"fields": {
"model": "poll",
"name": "poll",
"app_label": "polls"
}
},
{
"pk": 9,
"model": "contenttypes.contenttype",
"fields": {
"model": "choice",
"name": "choice",
"app_label": "polls"
}
},
{
"pk": 10,
"model": "contenttypes.contenttype",
"fields": {
"model": "cronjoblog",
"name": "cron job log",
"app_label": "django_cron"
}
},
{
"pk": "1g6s3bly0gu6pn7spllerdw1n8sus08a",
"model": "sessions.session",
"fields": {
"expire_date": "2013-09-04T05:48:32Z",
"session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
}
},
{
"pk": "j7ifcyfzfhentb5tx9492fphdf20g2qp",
"model": "sessions.session",
"fields": {
"expire_date": "2013-09-11T04:54:37Z",
"session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
}
},
{
"pk": "m21on2le3kti3ezexzei1vpox57x8qgv",
"model": "sessions.session",
"fields": {
"expire_date": "2013-09-05T06:29:00Z",
"session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
}
},
{
"pk": "x16t71c19o93kukwu5mvog1g76ynv3ip",
"model": "sessions.session",
"fields": {
"expire_date": "2013-09-04T07:07:46Z",
"session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
}
},
{
"pk": 1,
"model": "sites.site",
"fields": {
"domain": "example.com",
"name": "example.com"
}
},
{
"pk": 1,
"model": "polls.poll",
"fields": {
"pub_date": "2013-08-21T05:56:00Z",
"question": "What are you doing?"
}
},
{
"pk": 2,
"model": "polls.poll",
"fields": {
"pub_date": "2013-08-21T05:58:00Z",
"question": "What's on your mind?"
}
},
.
.
]
If anyone can help, I would be very appreciated :S
Upvotes: 1
Views: 1149
Reputation: 456
Finally after pulling out my hair for several days, I found the working solution.
crontab -e
Add my job directly, I typed:
0 3 * * * cp -pRu /home/path/to/my/project/file_dir /home/path/to/my/backup/dir
This will only copy new file/dir based on timestamps in file_dir to backup_dir everyday at 3 am
crontab -l
Hopefully, it helps someone who got similar problem :D
PS: thanks to Igor and zsquare as well.
Upvotes: 0
Reputation: 64573
See the format of django crontab:
#format 1
required: cron timing (in usual format: http://en.wikipedia.org/wiki/Cron#Format)
required: the python module path to the method
optional: a job specific suffix (f.e. to redirect out/err to a file, default: '')
#format 2
required: cron timing (in usual format: http://en.wikipedia.org/wiki/Cron#Format)
required: the python module path to the method
optional: list of positional arguments for the method (default: [])
optional: dict of keyword arguments for the method (default: {})
optional: a job specific suffix (f.e. to redirect out/err to a file, default: '')
In both cases you need to use python methods, not just commands from the shell. You are not allowed to write something like 'cd ...; ./command'; you should wrap this line into python module in your code.
More on it:
Upvotes: 1