Celery访问记录器文件 - [Errno 13]权限被拒绝:

时间:2017-07-20 15:19:54

标签: django ubuntu permissions celery supervisord

我在记录设置方面遇到了困难。我正在运行芹菜工作人员并使用主管打败。 芹菜位于 django 项目中。

我已经创建了一个文件夹logs,我希望拥有所有日志。

权限:

/日志/

drwxrwxrwx 2 django django     4096 Jul 20 15:03 logs

/日志/ any_file

total 1488
drwxrwxrwx 2 django django   4096 Jul 20 15:03 .
drwxrwxr-x 9 django django   4096 Jul 20 15:02 ..
-rw-r--r-- 1 root   root   762824 Jul 20 15:07 celeryb_err.log
-rw-r--r-- 1 root   root        0 Jul 20 15:03 celeryb_out.log
-rw-r--r-- 1 root   root   737880 Jul 20 15:07 celery_err.log
-rw-r--r-- 1 root   root        0 Jul 20 15:02 celery.log
-rw-r--r-- 1 root   root        0 Jul 20 15:03 celery_out.log
-rw-r--r-- 1 root   root        0 Jul 20 15:02 engine.log
-rw-r--r-- 1 root   root        0 Jul 20 15:02 scrapy.log

我认为这是因为每个日志文件都拥有root。

问题在于芹菜回归:

celeryb_err.log

Traceback (most recent call last):
  File "/home/django/realestate_scanner/realestate_scannervenv/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/__main__.py", line 14, in main
    _main()
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/base.py", line 281, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 480, in handle_argv
    return self.execute(command, argv)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/celery.py", line 412, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/base.py", line 285, in run_from_argv
    sys.argv if argv is None else argv, command)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/base.py", line 367, in handle_argv
    *self.parse_options(prog_name, argv, command))
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/base.py", line 403, in parse_options
    self.parser = self.create_parser(prog_name, command)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/base.py", line 419, in create_parser
    self.add_arguments(parser)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/bin/beat.py", line 114, in add_arguments
    '-s', '--schedule', default=c.beat_schedule_filename)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/utils/collections.py", line 130, in __getattr__
    return self[k]
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/utils/collections.py", line 431, in __getitem__
    return getitem(k)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/utils/collections.py", line 280, in __getitem__
    return mapping[_key]
  File "/home/django/realestate_scanner/realestate_scannervenv/lib/python2.7/UserDict.py", line 36, in __getitem__
    if key in self.data:
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/kombu/utils/objects.py", line 44, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/app/base.py", line 148, in data
    return self.callback()
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/app/base.py", line 910, in _finalize_pending_conf
    conf = self._conf = self._load_config()
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/app/base.py", line 920, in _load_config
    self.loader.config_from_object(self._config_source)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/loaders/base.py", line 133, in config_from_object
    self._conf = force_mapping(obj)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/celery/utils/collections.py", line 52, in force_mapping
    return DictAttribute(m) if not isinstance(m, Mapping) else m
  File "/home/django/realestate_scanner/realestate_scannervenv/lib/python2.7/abc.py", line 131, in __instancecheck__
    subclass = getattr(instance, '__class__', None)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/django/utils/functional.py", line 238, in inner
    self._setup()
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/django/realestate_scanner/realestate_scannervenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/django/realestate_scanner/realestate_scanner/settings.py", line 227, in <module>
    dictConfig(LOGGING)
  File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 576, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'celery': [Errno 13] Permission denied: '/home/django/realestate_scanner/logs/celery.log'

settings.py

# worker_hijack_root_logger = False

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'simple': {
            'format': '%(levelname)s dc %(message)s',
            'datefmt': '%y %b %d, %H:%M:%S',
        },
        'normal': {
            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            'datefmt': '%y %b %d, %H:%M:%S',
        },

    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        # 'django': {
        #     'level': 'INFO',
        #     'class': 'logging.handlers.RotatingFileHandler',
        #     'filename': 'logs/django.log',
        #     'formatter': 'normal',
        #     'maxBytes': 1024 * 1024 * 100,  # 100 mb
        # },
        'celery': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/celery.log',
            'formatter': 'normal',
            'maxBytes': 1024 * 1024 * 100,  # 100 mb
        },
        'engineapp': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/engine.log',
            'formatter': 'normal',
            'maxBytes': 1024 * 1024 * 100,  # 100 mb
        },
        'scrapy': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/scrapy.log'),
            'formatter': 'normal'
        }
    },
    'loggers': {
        # 'django': {
        #     'handlers': ['django',],
        #     'level': 'INFO',
        # },
        'scrapy': {
            'handlers': ['scrapy'],
            'level': 'INFO',
        },
        'engineapp': {
            'handlers': ['engineapp'],
            'level': 'INFO',
        },
        'celery': {
            'handlers': ['engineapp'],
            'level': 'INFO',
        },
    },

}

from logging.config import dictConfig

dictConfig(LOGGING)

celery.py

from __future__ import absolute_import
import os

import logging
from logging.handlers import RotatingFileHandler

from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'realestate_scanner.settings')
app = Celery('realestate_scanner')


app.config_from_object('django.conf:settings',)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


import celery.signals

@celery.signals.setup_logging.connect
def on_celery_setup_logging(**kwargs):
    logger = logging.getLogger('celery')
    logger.setLevel(logging.INFO)
    handler = RotatingFileHandler('logs/celery.log', maxBytes=1024 * 1024 * 10, backupCount=2, )
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    handler.setLevel(logging.INFO)
    logger.handlers = [handler]

主管/ conf.d / celery_worker.conf

[program:celery_worker]
user=django
command=/home/django/realestate_scanner/realestate_scannervenv/bin/celery worker -A realestate_scanner --loglevel=INFO --purge
directory=/home/django/realestate_scanner
autostart=true
autorestart=true
stderr_logfile=/home/django/realestate_scanner/logs/celery_err.log
stdout_logfile=/home/django/realestate_scanner/logs/celery_out.log

主管/ conf.d / celery_beat.conf

[program:celery_beat]
user=django
command=/home/django/realestate_scanner/realestate_scannervenv/bin/celery beat -A realestate_scanner --loglevel=INFO
directory=/home/django/realestate_scanner
autostart=true
autorestart=true
stderr_logfile=/home/django/realestate_scanner/logs/celeryb_err.log
stdout_logfile=/home/django/realestate_scanner/logs/celeryb_out.log

你知道怎么摆脱这个错误吗?

1 个答案:

答案 0 :(得分:0)

允许在shell中编写以下命令的celery日志文件。

sudo chmod 777 /home/django/realestate_scanner/logs/celery.log

如果您希望在删除文件后使用它。

sudo chmod 777 /home/django/realestate_scanner/logs