我正在开发 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
工作示例
设置.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