简体   繁体   English

如何在 Laravel 项目的共享主机上正确设置 cron 作业?

[英]How do I correctly set up a cron job on shared hosting for a Laravel project?

I set up a cron job to run 'php artisan schedule:run' on Namecheap.我设置了一个 cron 作业来在 Namecheap 上运行“php artisan schedule:run”。 Running the cron job in the Terminal triggers the scheduled commands and outputs the result for each or states that no commands are ready to run when none are, but on its own, it doesn't seem to run.在终端中运行 cron 作业会触发预定的命令并输出每个命令的结果,或者表示没有命令准备好运行,但就其本身而言,它似乎没有运行。

Namecheap says the cron ran but may not have triggered the commands, and that the problem may be with my scripts. Namecheap 说 cron 运行但可能没有触发命令,问题可能出在我的脚本上。 According to them,根据他们,

script wasn't started, may be caused due to incorrect settings between your script and created cron job.脚本未启动,可能是由于脚本和创建的 cron 作业之间的设置不正确造成的。

I did get an email with this content though:不过,我确实收到了一个包含以下内容的 email:

In ArgvInput.php line 264:
                                           
  Invalid argument supplied for foreach()  
                                           

X-Powered-By: PHP/7.2.34
Content-type: text/html; charset=UTF-8

which may mean that the scripts were triggered, but I have no idea what this points to.这可能意味着脚本被触发了,但我不知道这指向什么。

My expectation is that similar to the Terminal output, the email should provide information on each command that ran (or that didn't run) and the output corresponding to each one.我的期望是,类似于终端 output,email 应该提供有关运行(或未运行)的每个命令的信息以及与每个命令对应的 output。 I don't understand how my script could be the issue if they ran when called from the Terminal.我不明白如果它们在从终端调用时运行,我的脚本怎么会成为问题。

My cron job:我的计划任务:

0   15  *   *   *   /usr/local/bin/php /home/user-name/project-folder/artisan schedule:run

Several other ways of specifying the cron job also work such as:其他几种指定 cron 作业的方法也可以使用,例如:

0   15  *   *   * cd /home/user-name/project-folder && php artisan schedule:run

What can I do to get this working as expected.我该怎么做才能使它按预期工作。

Ensuring that the timezone in the app (config/app.php) matches with the server time resolved this issue for me.确保应用程序 (config/app.php) 中的时区与服务器时间匹配为我解决了这个问题。 With this, once the cron job runs, any due commands will be triggered.有了这个,一旦 cron 作业运行,任何到期的命令都会被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM