Add Laravel task to cron with parameters

那年仲夏 提交于 2019-12-12 03:54:09

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/15899315/add-laravel-task-to-cron-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!