我正在开发django-python项目并坚持使用django-crontab进行自动文件备份,这是代码:
在我的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'),
]
在myscript.sh中
#!/bin/sh
set -e
cd /home/path/to/my/project/
exec cp file some_dir/copy_file
我在我的服务器上测试此代码以便在15:25运行并且它没有执行myscript.sh
脚本只是将副本文件复制到另一个目录(用于备份),我在服务器中使用./myscript.sh手动测试,脚本运行良好...
我一直在尝试各种方法,但似乎没有任何作品
我从django-crontab(https://pypi.python.org/pypi/django-crontab/0.5.1)和此网站(https://superuser.com/questions/81262/how-to-execute-shell-script-via-crontab)+ http://github.com/kraiz/django-crontab
中获取引用修改
我也使用此命令添加我的cronjob
python manage.py crontab add
我已经使用此命令检查了作业
python manage.py crontab show
工作示例
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?"
}
},
.
.
]
如果有人可以提供帮助,我将非常感激:S
答案 0 :(得分:1)
在您运行python manage.py crontab add
之前,您在设置中安排的cronjobs实际上并未添加到crontab中。
答案 1 :(得分:1)
请参阅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: '')
在这两种情况下,您都需要使用 python方法,而不仅仅是来自shell的命令。 你不能写'cd ......; 。/命令';你应该在代码中将这一行包装成python模块。
更多信息:
答案 2 :(得分:0)
最后,拔掉我的头发好几天后,我找到了可行的解决方案。
crontab -e
直接添加我的工作,我输入:
0 3 * * * cp -pRu / home / path / to / my / project / file_dir / home / path / to / my / backup / dir
这样每天凌晨3点只会根据file_dir中的时间戳复制新的文件/目录到backup_dir
crontab -l
希望它有助于遇到类似问题的人:D
PS:感谢Igor和zsquare。