Celery as a service - PermissionError: [Errno 13] Permission denied: '/var/run/celery'

Viewed 1213

I'm trying to create a service that runs celery but I came across permissions problem. I saw in many tutorials that pidfile path is /var/run/celery/%n.pid but it seems like my user doesn't have rights to write to run.

When I start the service, this is what it returns:

PermissionError: [Errno 13] Permission denied: '/var/run/celery' 

celery.service

[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=master
Group=master
EnvironmentFile=/etc/conf.d/celery
WorkingDirectory=/home/master/myproject/
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
  -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
  --pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
  -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

[Install]
WantedBy=multi-user.target

celery.conf

CELERYD_NODES="celery-worker"

CELERY_BIN="/home/master/.virtualenvs/myproject/bin/celery"

# App instance to use
CELERY_APP="myproject"


CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=3"

# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_LEVEL="INFO"

How to make it work?

2 Answers

I saw the same problem, and it turned out that I created /var/log/celery and /var/run/celery with root privilege, it looked like this

drwxr-xr-x 2 root root 6 Aug  4 09:40 /var/log/celery

but I specified in /etc/default/celeryd my celeryd worker run as some non-root user, assume me, so me has no write/execute permission to /var/log/celery, so it got permission denied.

You may either create your /var/log/celery directory under user master group master or grant write/execute to user master (simply run chmod o+w /var/log/celery may be insecure, though I did in testing and hopefully it worked.

Dont know if it is the best approach, but here is how i solved mine...

$sudo mkdir /var/run/celery

user and group don't necessarily have to be celery..

$sudo chown -R celery:celery /var/run/celery
$sudo chmod o+w /var/run/celery

and then

$sudo service celery restart
Related