Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django#u crontab正在添加我的工作,但它们没有';他似乎没有执行。有解决办法吗?_Django_Ubuntu_Cron_Gunicorn_Django Cron - Fatal编程技术网

django#u crontab正在添加我的工作,但它们没有';他似乎没有执行。有解决办法吗?

django#u crontab正在添加我的工作,但它们没有';他似乎没有执行。有解决办法吗?,django,ubuntu,cron,gunicorn,django-cron,Django,Ubuntu,Cron,Gunicorn,Django Cron,我在Ubuntu20上运行了一个django项目,其中使用了django crontab() 在目录中,我添加了一个cron.py文件: from .models import <my_model> from datetime import datetime def remove_stamps(): for stamp in <my_model>.objects.order_by('-stop_date'): if stamp.can_be_r

我在Ubuntu20上运行了一个django项目,其中使用了django crontab()

在目录中,我添加了一个cron.py文件:

from .models import <my_model>
from datetime import datetime


def remove_stamps():
    for stamp in <my_model>.objects.order_by('-stop_date'):
        if stamp.can_be_removed():
            stamp.delete()
        else:
            break


def close_stamps():
    for stamp in <my_model>.objects.filter(stop_date=None):
        stamp.stop_date = datetime.now()
        stamp.save()
使用
journalctl\u COMM=cron--since=“2021-5-1 14:00”
检查作业是否运行,给出以下信息:

...
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276942]: pam_unix(cron:session): session opened for user <new_user> by (uid=0)
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276940]: pam_unix(cron:session): session opened for user <new_user>by (uid=0)
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276946]: (<new_user>) CMD (/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py crontab run <HASH KEY>>
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276947]: (<new_user>) CMD (/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py crontab run <HASH KEY>>
May 01 17:00:08 ubuntu-2gb-hel1-2 CRON[276945]: pam_unix(cron:session): session closed for user <new_user>
May 01 17:00:08 ubuntu-2gb-hel1-2 CRON[276942]: pam_unix(cron:session): session closed for user <new_user>
...
。。。
5月1日17:00:01 ubuntu-2gb-hel1-2 CRON[276942]:pam_unix(CRON:session):会话由用户打开(uid=0)
5月1日17:00:01 ubuntu-2gb-hel1-2 CRON[276940]:pam_unix(CRON:session):会话由用户打开(uid=0)
五月一日17:00:01 ubuntu-2gb-hel1-2 CRON[276946]:()CMD(/opt//venv/bin/python3/opt//manage.py crontab run>
五月一日17:00:01 ubuntu-2gb-hel1-2 CRON[276947]:()CMD(/opt//venv/bin/python3/opt//manage.py crontab run>
5月1日17:00:08 ubuntu-2gb-hel1-2 CRON[276945]:pam_unix(CRON:session):用户会话已关闭
5月1日17:00:08 ubuntu-2gb-hel1-2 CRON[276942]:pam_unix(CRON:session):用户会话已关闭
...
我认为cronjob运行正常,但没有执行脚本。但我不知道如何修复。我完全被卡住了。欢迎提供所有帮助


我还尝试使用
crontab-e
添加一个cronjob,但后来我陷入了如何在virtualenv中使用它的困境。

我找到了一个解决方案,可以将cron job添加到我的django项目中。我使用了django命令()。如您所见,我在
中创建了一个
管理/commands/
文件夹(部分
)并添加了命令脚本文件

<my_django_app>/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            <my_command_script>.py
    tests.py
    views.py
我可以使用以下脚本执行该命令:

PYTHONPATH=/etc/opt/<project_name>:/opt/<project_name> DJANGO_SETTINGS_MODULE=settings su <new_user> -c "/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py <my_command_script>"
接下来,我使用以下命令使该文件可执行:

chmod 755 /<my_folder>/<my_cron_file>.sh
现在脚本每2分钟执行一次。它似乎工作正常

我不确定这是正确的方式,所以如果有人看到问题,请让我知道

<HASH KEY> -> ('*/4 * * * *', '<my_django_app>.cron.remove_stamps')
<HASH KEY> -> ('*/5 * * * *', '<my_django_app>.cron.close_stamps')
...
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276942]: pam_unix(cron:session): session opened for user <new_user> by (uid=0)
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276940]: pam_unix(cron:session): session opened for user <new_user>by (uid=0)
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276946]: (<new_user>) CMD (/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py crontab run <HASH KEY>>
May 01 17:00:01 ubuntu-2gb-hel1-2 CRON[276947]: (<new_user>) CMD (/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py crontab run <HASH KEY>>
May 01 17:00:08 ubuntu-2gb-hel1-2 CRON[276945]: pam_unix(cron:session): session closed for user <new_user>
May 01 17:00:08 ubuntu-2gb-hel1-2 CRON[276942]: pam_unix(cron:session): session closed for user <new_user>
...
<my_django_app>/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            <my_command_script>.py
    tests.py
    views.py
from django.core.management.base import BaseCommand, CommandError
from <my_django_app>.models import <my_model>
from datetime import datetime


class Command(BaseCommand):
    help = 'Closes stamps that have NO end date'

    def handle(self, *args, **options):
        try:
            stamps = <my_model>.objects.filter(date=None)
        except stamps.DoesNotExist:
            raise CommandError('No stamps available to close')

        for stamp in stamps:
            stamp.date = datetime.now()
            stamp.save()

        self.stdout.write(self.style.SUCCESS('Successfully closed the stamps'))
PYTHONPATH=/etc/opt/<project_name>:/opt/<project_name> DJANGO_SETTINGS_MODULE=settings su <new_user> -c "/opt/<project_name>/venv/bin/python3 /opt/<project_name>/manage.py <my_command_script>"
#!/bin/bash
export PYTHONPATH=/etc/opt/<project_name>:/opt/ <project_name>
export DJANGO_SETTINGS_MODULE=settings
su <new_user> -c "/opt/<project_name>/venv/bin/python /opt/<project_name>/manage.py <my_command_script>"
chmod 755 /<my_folder>/<my_cron_file>.sh
*/2 * * * * /<my_folder>/<my_cron_file>.sh