crond:无法设置组:操作不允许

crond:无法设置组:操作不允许

今天早上,我将 PHP 版本升级到 7.1,并在 cron 尝试运行php /var/www/html/artisan schedule:run(一个简单的 PHP 命令)时发现一个问题,我看到输出:

3/3/2017 10:39:00 AMcrond: can't set groups: Operation not permitted
3/3/2017 10:39:00 AMcrond: USER www-data pid 1562 cmd php /var/www/html/artisan schedule:run
3/3/2017 10:40:00 AMcrond: can't set groups: Operation not permitted
3/3/2017 10:40:00 AMcrond: USER www-data pid 1563 cmd php /var/www/html/artisan schedule:run
3/3/2017 10:41:00 AMcrond: can't set groups: Operation not permitted
3/3/2017 10:41:00 AMcrond: USER www-data pid 1564 cmd php /var/www/html/artisan schedule:run
3/3/2017 10:42:00 AMcrond: can't set groups: Operation not permitted
3/3/2017 10:42:00 AMcrond: USER www-data pid 1565 cmd php /var/www/html/artisan schedule:run
3/3/2017 10:43:00 AMcrond: can't set groups: Operation not permitted
3/3/2017 10:43:00 AMcrond: USER www-data pid 1566 cmd php /var/www/html/artisan schedule:run

正在运行的命令是 Laravel artisan 命令。它每分钟运行一次,允许在应用程序本身内完成其他计划工作。此命令中没有任何内容写入任何文件或类似内容。计划工作与数据库通信并发送一些电子邮件。由于它是一个 Docker 容器,因此应用程序日志被发送到 stdout。

cron使用命令在容器中运行crond -f -d 8。以下是 Dockerfile:

# This container should be used for any/all CLI processes
# including cron, queues, etc.
FROM php:7.1-alpine

# Copy the application files to the container
ADD . /var/www/html

WORKDIR /var/www/html

# fix permissions in CI
RUN sed -ri 's/^www-data:x:82:82:/www-data:x:1000:1000:/' /etc/passwd \
    && sed -ri 's/^www-data:x:82:/www-data:x:1000:/' /etc/group

# Install Composer dependencies
RUN apk add --update --no-cache git zip unzip \

        # needed for spatie/laravel-backup
        mysql-client \

        # needed for gd
        libpng-dev libjpeg-turbo-dev \

    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN docker-php-ext-install pdo_mysql gd \

        # needed for forking processes in laravel queues as of Laravel 5.3
        pcntl

# Ownership of the app dir for www-data
RUN chown -R www-data:www-data /var/www/html /home/www-data/

# Put php artisan schedule:run in a crontab
RUN echo "*       *       *       *       *       php /var/www/html/artisan schedule:run" > /etc/crontabs/www-data

# Make sure when users get into the container they aren't root
USER www-data

我已经排除了这php artisan schedule:run是原因,因为我可以手动运行它并且一切正常。这意味着这是 cron 中的问题。

cron 在幕后做了什么可能导致这个错误?

答案1

这是因为根据以下两个条件之一man 2 setgroups

   EPERM  The calling process has insufficient privilege (the caller
          does not have the CAP_SETGID capability in the user namespace
          in which it resides).

   EPERM (since Linux 3.19)
          The use of setgroups() is denied in this user namespace.  See
          the description of /proc/[pid]/setgroups in
          user_namespaces(7).

我猜想您没有使用用户命名空间,在这种情况下,docker 容器中不允许使用 CAP_SETGID 功能。您需要更改容器功能集来修复此问题。

答案2

您正在运行 cron,只是为了在后台运行此命令:

RUN echo "*       *       *       *       *       php /var/www/html/artisan schedule:run" > /etc/crontabs/www-data

用 sh 替换 cron 应该可以让你 /不/ 诉诸增加容器上限:

  artisan_schedule_run:
    image: your-app-image
    command: /dumb-init /bin/sh -c "while true; do su www-data -c \"php /var/www/html/artisan schedule:run\" & sleep 60; done"

相关内容