通过docker中的supervisor执行PHP脚本

I want to execute a PHP script in my docker container on a regularly basis, let's say every 5 minutes - with supervisord controlling.

My 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

My crontab file:

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

What happens, when I start my server:

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
...

The server is running & it looks like the cronjob is working as expected, but the output that I expect after the php script got executed (creation of a file) does not happen.

I for sure cross checked my command:

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

It will result in the creation of the file I expect the script to create (it takes some time because lots of data is fetched) and it will output whatever was echoed inside the PHP into my terminal.

Do you have an idea what could go wrong?

duanhai4046
duanhai4046 (无论如何,感谢您的关注和暗示。)我已经写过手动执行文件时会发生什么:脚本完成它的工作,它应创建的文件已经创建。被调用的php文件以#开头!/usr/local/bin/php它唯一能做的就是调用一些API,将结果保存在json中并将结果保存到/var/www/html/market/res/脚本输出的可用文件夹中(之后)手动调用)只显示文件保存后在php文件中回显的内容:“下载完成”
大约 2 年之前 回复
dqluw20882
dqluw20882 仍然是一个坏主意,但是,这是你的问题。顺便说一句:代码是由PHP执行还是可以用BASH或POSIX(sh)shell脚本替换它是否相关?
大约 2 年之前 回复
dtm41506
dtm41506 不用担心,无论如何都是为了原型设计。
大约 2 年之前 回复
duanbi8529
duanbi8529 我相信PHP5支持将在本月或下个月结束。甚至不要想到用这个开始新的东西。也就是说,如果你自己在没有supervisord的情况下执行代码会发生什么?什么是skript的输出(使用>>/var/log/script.log2>&1)?考虑到最小可重复示例的要求,脚本的内容是什么样的?
大约 2 年之前 回复

1个回答

It's not a Docker problem, it's your settings problem. What your /usr/local/bin/php fetchStock.php >/dev/null 2>&1 does is explained in detail in here, I will copy it over:

>/dev/null redirects standard output (stdout) to /dev/null, which discards it.

...

2>&1 redirects standard error (2) to standard output (1), which then discards it as well since standard output has already been redirected.

You should keep only /usr/local/bin/php fetchStock.php...

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐