Running Symfony command in cron, but environment variables are missing.

I am trying to run a Symfony command every minute, but the environment variables are not loaded. I am doing it as follows right now:

* * * * * root php /var/www/html/bin/console app:run-scheduler > /proc/1/fd/1 2>/proc/1/fd/2

The cron file is copied in the Dockerfile:

cp fly/cron /etc/cron.d/send-test-message

The file is properly copied and the command is also correct. The problem is that within bin/console the environment variables aren’t present.

So I am getting errors like this:

That’s because only in dev the profiler is loaded, and dev is the default APP_ENV for Symfony.

Any ideas how I can get the environment variables within bin/console?

I am running it in a separate VM:

[processes]
  app = ''
  worker = 'php bin/console messenger:consume -vvv'
  scheduler = 'cron -f'
1 Like

I solved this by following two steps:

  • I moved ALL environment variables from fly.toml to a .env file in my docker/ directory. In the Dockerfile I copy this file to the root directory of my project.
  • Then in my entrypoint I append all the secrets to this .env file:
printf "\n# Secrets" >> .env
printf "\nAPP_SECRET='%s'" $APP_SECRET >> .env
printf "\nMESSENGER_TRANSPORT_DSN='%s'" $MESSENGER_TRANSPORT_DSN >> .env
printf "\nPOSTMARK_API_KEY='%s'" $POSTMARK_API_KEY >> .env

if [ $# -gt 0 ];then
    # If we passed a command, run it as root
    exec "$@"
else
    chown -R webuser:webgroup /var/www/html

    exec /init
fi
1 Like