通过Docker中的主管执行PHP脚本

时间:2018-08-26 09:07:35

标签: php docker cron supervisord

我想定期在我的docker容器中执行一个PHP脚本,比如说每5分钟执行一次-具有监督控制。

我的Dockerfile:

FROM php:5.6-apache
RUN apt-get update && apt-get install -y \
    cron \
    rsyslog \
    supervisor

COPY supervisord/*.conf /etc/supervisor/conf.d/
COPY crontab /etc/cron.d/cron
RUN rm -Rf /etc/cron.daily  && \
    rm -Rf /etc/cron.weekly && \
    rm -Rf /etc/cron.monthly && \
    rm -Rf /etc/cron.hourly && \
    chmod a+x /etc/cron.d/cron

EXPOSE 80

CMD exec supervisord -n

我的crontab文件:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/5 * * * * root cd /var/www/html/market/autoFetch && /usr/local/bin/php fetchStock.php >/dev/null 2>&1

启动服务器后会发生什么:

php_1  | /usr/lib/python2.7/dist-packages/supervisor/options.py:298: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.    
php_1  |   'Supervisord is running as root and it is searching '
php_1  | 2018-08-26 08:45:52,515 CRIT Supervisor running as root (no user in config file)
php_1  | 2018-08-26 08:45:52,516 INFO Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
php_1  | 2018-08-26 08:45:52,522 INFO RPC interface 'supervisor' initialized
php_1  | 2018-08-26 08:45:52,523 CRIT Server 'unix_http_server' running without any HTTP authentication checking
php_1  | 2018-08-26 08:45:52,523 INFO supervisord started with pid 1
php_1  | 2018-08-26 08:45:53,526 INFO spawned: 'cron' with pid 8
php_1  | 2018-08-26 08:45:53,528 INFO spawned: 'rsyslogd' with pid 9
php_1  | 2018-08-26 08:45:53,531 INFO spawned: 'apache2' with pid 10
php_1  | 2018-08-26 08:45:54,607 INFO success: cron entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
php_1  | 2018-08-26 08:45:54,607 INFO success: rsyslogd entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
php_1  | 2018-08-26 08:45:54,607 INFO success: apache2 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
php_1  | 2018-08-26 09:00:11,493 INFO reaped unknown pid 21
...

服务器正在运行,并且cronjob看起来像预期的那样工作,但是在执行php脚本(创建文件)后,我期望的输出没有发生。

我确定交叉检查了我的命令:

cd /var/www/html/market/autoFetch && /usr/local/bin/php fetchStock.php >/dev/null 2>&1

这将导致创建我希望脚本创建的文件(这会花费一些时间,因为会提取大量数据),并且它将在PHP中回显的任何内容输出到我的终端。

您知道什么地方可能出问题吗?

1 个答案:

答案 0 :(得分:0)

这不是Docker问题,这是您的设置问题。 in detail in here说明了您的/usr/local/bin/php fetchStock.php >/dev/null 2>&1的操作,我将其复制到以下位置:

  

>/dev/null将标准输出(stdout)重定向到/dev/null,并丢弃它。

     

...

     

2>&1将标准错误(2)重定向到标准输出(1),由于标准输出已被重定向,该错误也被丢弃。

您应仅保留/usr/local/bin/php fetchStock.php ...