0

I want to add a Laravel task into cron, this is what I use to run it from command line (and runs succesfully)

php artisan cron:hourly --env=staging

Translated into cron:

/usr/bin/php -q /home/usr/public_html/staging/artisan cron:hourly --env=staging

I assume there is a problem with the parameter --env=staging because I got an error when the cron is executed (without this parameter I can't run the task in staging environment):

Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost'

Could anyone explain me the the right syntax to execute the laravel task in cron?

Update

Actually, the problem is only happening if I place the cron command inside an SH script. Due to unknown reason, the script does not send "--env=staging" argument, and this ends on the error described.

xavierhb
  • 76
  • 3
  • 8

2 Answers2

2

The error message suggests that something with the environnement ist not set properly. I'm not sure why there is a problem though.

Please see my crontab file, for reference. This works on my debian linux installation. Also note the -f flag, this could be the problem.

0 23 * * * /usr/bin/php -q -f /home/usr/demo/public_html/artisan mytask --env=live

On my Server, the php-cli does output all PHP-Notices and therefor sends me a lot of Emails. So you should probably check for PHP-Notices and/or disable them by setting error_reporting(E_ERROR);.

aebersold
  • 11,286
  • 2
  • 20
  • 29
  • I have quite the same command: `/usr/bin/php -q -f /home/user/public_html/staging/artisan cron:every_hour --env=staging ` but it is not working, I'm wondering if it's because the `cron:every_hour` format that is breaking the whole command. – xavierhb Jun 05 '13 at 12:59
  • No, it doesn't, keep saying the same error `Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)'`. The error is at this point, but I can't find the right syntax. – xavierhb Jun 05 '13 at 13:46
  • This means that the PHP script is executed, but the environnement is not set. Does it work, if you run the task on the shell? Can you try a task without a colon `:` symbol? – aebersold Jun 06 '13 at 09:13
0

The error indicates that there is no database connection configured for the staging environment. The bit with ''@'localhost' should contain the username for the db connection. Ie: 'a_db_user'@'localhost'. Because it is empty we can tell that no db user has been configured. The db config will need to be set in application/config/database.php or application/config/staging/database.php

Collin James
  • 9,062
  • 2
  • 28
  • 36
  • Thank you, you are right, the problem is that the param `--env=staging` is not being received by Laravel. I am looking for a solution. – xavierhb Jun 05 '13 at 13:52