webdevops / php-docker-boilerplate

:stew: PHP Docker Boilerplate for Symfony, Wordpress, Joomla or any other PHP Project (NGINX, Apache HTTPd, PHP-FPM, MySQL, Solr, Elasticsearch, Redis, FTP)
https://webdevops.io/projects/php-docker-boilerplate/
MIT License
561 stars 185 forks source link

Configure crontab #70

Closed nverhoye closed 2 years ago

nverhoye commented 6 years ago

Hi, i'm trying to configure a task in crontab

File : /etc/cron/crontab

# m h  dom mon dow  user  command
* * * * * php /app/foo.php

File is correctly copyed when i launch docker-compose, see :

RUN ln -sf /opt/docker/etc/cron/crontab /etc/cron.d/docker-boilerplate \
    && chmod 0644 /opt/docker/etc/cron/crontab \
    && echo >> /opt/docker/etc/cron/crontab \
    && ln -sf /opt/docker/etc/php/development.ini /opt/docker/etc/php/php.ini

But it not works. Also when i laugh manually /app/foo.php it works.

Info : foo.php is a file which write content on another file (log)

Where is the problem please ? Sorry for my bad english. Thanks.

kyrilg commented 6 years ago

Hi @nverhoye, Environments differs between shell and crontab, you can check it with printenv in shell / crontab

https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user

I use approach 2

Try something like this: * * * * * application . ~/.profile; /usr/local/bin/php /app/foo.php

mblaschke commented 6 years ago

we should implement go-crond to get rid of the environment issue :)

nverhoye commented 6 years ago

Hi @kyrilg

Thanks for your reply, but it still does not work unfortunately.

I pushed * * * * * application . ~/.profile; /usr/local/bin/php /app/foo.php

Instead of

# m h  dom mon dow  user  command
* * * * * php /app/foo.php

No result :(

mblaschke commented 6 years ago

please make sure you have an empty line at the end (cron requirement).. otherwise your crontab is ignored, also make sure the permissions are correct and not world writeable.

go-crond will fix this with correct error messages.

nverhoye commented 6 years ago

Hi @mblaschke, i have an empty line at the end (cron requirement), also permissions are correct (0644).

Below crontab used now :

* * * * * application . /home/application/.profile; /bin/bash /app/scheduler_cron.sh --mode always >> /app/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

NOW file is executed, but file can't do works asked. I have message returned : "Could not find a binary for php"

This is content of scheduler_cron.sh :

#!/bin/bash

# Generate an error if any variable doesn't exist
set -o nounset

# Location of the php binary
PHP_BIN=$(which php || true)

if [ -z "${PHP_BIN}" ]; then
    echo "Could not find a binary for php" 1>&2
    exit 1
fi

In cron task "which php" return empty BUT when i'm connected to docker's container it return '/usr/local/bin/php'

See :

docker exec -it app bash
root@03dc0dd62e77:/$ su application
application@03dc0dd62e77:/$ which php
/usr/local/bin/php

Any ideas ? Thanks

mblaschke commented 6 years ago

The environment parameters are not passed to cronjobs. They are filtered by default and AFAIK there is no way to fix this other then replacing the cron daemon.

I will replace cron with gocrond this weeks to get rid of this stupid issue

nverhoye commented 6 years ago

Hi @mblaschke, can you share the solution ? Thanks

abagayev commented 4 years ago

Hey, I just created an example of how to use crontab with webdevops docker-compose, you can check it our here: https://github.com/abagayev/docker-bootstrap-collection/tree/master/crontab-in-container