将参数添加到cron中的Laravel任务 [英] Add Laravel task to cron with parameters
问题描述
我想在cron中添加一个Laravel任务,这是我使用它从命令行运行(并且运行成功)
php artisan cron:hourly --env = staging
翻译成cron:
/ usr / bin / php -q / home / usr / public_html / staging / artisan cron:hourly --env = staging
我假设参数 - env = staging
因为我在cron执行时遇到错误(没有此参数我无法在临时环境中运行任务):
未捕获具有消息'SQLSTATE [HY000]的异常'PDOException'[1045]拒绝用户''@'localhost'的访问
在cron中执行laravel任务的正确语法?
更新
实际上,问题只发生在我放置cron命令在SH脚本内。由于未知原因,脚本不会发送--env = staging参数,并且结束于描述的错误。
解决方案 p>错误消息表明与环境的东西没有正确设置。我不知道为什么有问题,虽然。
请参阅我的crontab文件,以供参考。这工作对我的debian linux安装。还要注意 -f
标志,这可能是问题。
0 23 * * * / usr / bin / php -q -f / home / usr / demo / public_html / artisan mytask --env = live
在我的服务器上,php-cli输出所有PHP通知,因此发送了很多电子邮件。所以你应该通过设置 error_reporting(E_ERROR);
来检查PHP-Notices和/或禁用它们。
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.
解决方案 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);
.
这篇关于将参数添加到cron中的Laravel任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!