0

There are a few questions covering this issue but none of them are solving my problem. This is a Symfony 2.8 application running in a PHP 7.1 docker container.

When the command is run as cron, the absolute URLs are using localhost in emails. I've tried both suggestions in the documentation here - configuring on the command itself and globally in parameters.

Everything works until the command is run by cron - even if I run the same manually. For example, running the following on the host into the container correctly generates the absolute URLs:

docker-compose exec --user www-data application sh -c "cd /var/www/html && /usr/local/bin/php app/console my:command -e=stage"

But my cron is reverting to localhost:

1-59/3 * * * * www-data cd /var/www/html && /usr/local/bin/php app/console my:command -e=stage

Cron is running inside the container. If I bash into the container as www-data and run: /usr/local/bin/php app/console my:command -e=stage it also works correctly so I'm at a loss as to why it's not playing ball as a crontab.

1 Answer 1

0

Environment variables are not available to cron. So the command and parameters were using a BASE_DOMAIN environment variable which was null when being run by a scheduled cron task.

To get around the issue, the base domain was passed as an argument to the command and the host was set on the request context as follows:

$context = $this->router->getContext();
$context->setHost($input->getArgument('host'));

And then updating the cron to include the host.

Sign up to request clarification or add additional context in comments.

3 Comments

An incomplete answer in terms of code and the command. How to pass the base domain via command ?
Incidentally, I seem to have had this exact problem twice and come up with two solutions! stackoverflow.com/questions/52970558/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.