Crontab Jobs没有执行shell脚本

时间:2013-08-26 09:00:19

标签: django shell python-2.7 crontab

我正在开发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

3 个答案:

答案 0 :(得分:1)

在您运行python manage.py crontab add之前,您在设置中安排的cronjobs实际上并未添加到crontab中。

RTD

答案 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)

最后,拔掉我的头发好几天后,我找到了可行的解决方案。

  1. 我使用putty(SSH)
  2. 登录服务器
  3. 类型命令:crontab -e
  4. 您将看到crontab作业列表
  5. 直接添加我的工作,我输入:

    0 3 * * * cp -pRu / home / path / to / my / project / file_dir / home / path / to / my / backup / dir

  6. 这样每天凌晨3点只会根据file_dir中的时间戳复制新的文件/目录到backup_dir

    1. 完成输入后,按ESC,然后按CTRL + x,它会询问您是否要保存,只需输入Y然后按ENTER键保存文件。
    2. 按类型检查所有crontab作业:crontab -l
    3. 就是这样!干杯男!
    4. 希望它有助于遇到类似问题的人:D

      PS:感谢Igor和zsquare。