简体   繁体   English

在1&1共享主机上运行Cronjob [Laravel]

[英]Running a Cronjob on 1&1 Shared Hosting [Laravel]

In my Laravel application, I have created a scheduled task, as defined in a previous question that I asked . 在我的Laravel应用程序中,我创建了一个计划任务,该任务是在我之前提出的问题中定义的。 It essentially grabs data and updates a few tables. 它实质上是获取数据并更新一些表。

I followed a help article on 1&1 about creating cronjobs . 我遵循了关于1&1的有关创建cronjobs的帮助文章。

To summarise this article it said the following. 在总结本文时,它说了以下内容。

SSH to your server, that you want to set up the new cronjob on, use whereis php to find the location of your PHP Binary, then use the crontab command to access the crontab and add a new job. SSH到要在其上设置新cronjob的服务器,使用whereis php查找PHP Binary的位置,然后使用crontab命令访问crontab并添加新作业。

This will require the PHP path and the path to the script that is to be run. 这将需要PHP路径和要运行的脚本的路径。

The issue is when I run that command I get the following. 问题是当我运行该命令时,我得到以下信息。

No crontab for [username] [用户名]没有crontab

So, I then Googled and it seems some shared hosts don't allow you to use or even access the crontab. 因此,我随后搜索了Google,似乎某些共享主机不允许您使用甚至访问crontab。

On my shared hosting server the path to php-7.1-cli is as follows /usr/bin/php7.1-cli 在我的共享托管服务器上, php-7.1-cli的路径如下/usr/bin/php7.1-cli

Now, Artisan is part of Laravel and requires a higher version of PHP than what is found in \\usr\\bin\\php 现在,Artisan是Laravel的一部分,并且需要比\\ usr \\ bin \\ php中更高版本的PHP

Given this, for the cronjob I thought I'd be able to do something like this: 鉴于此,对于cronjob,我认为我可以执行以下操作:

* * * * * /usr/bin/php7.1-cli /path/to/artisan schedule:run >> /dev/null 2>&1

Or 要么

* * * * * /usr/bin/php7.1-cli /path/to/mynewable/artisan schedule:run >> /dev/null 2>&1

My folder structure is as follows: 我的文件夹结构如下:

root
    /mynewable

But when I run either of these commands, nothing happens. 但是,当我运行这些命令中的任何一个时,都不会发生任何事情。

1&1 also has an inbuilt cron job creator but this only allows me to specify a URL path to a file that would run the necessary script. 1&1还具有内置的cron作业创建器,但这仅允许我指定将运行必要脚本的文件的URL路径。

在此处输入图片说明

Am I being prevented from using crontab? 是否可以阻止我使用crontab? If so, giving a URL seems impractical as the user wouldn't need to validate, but making a path in web routes to run the Laravel scheduler seems a bit of a security hole. 如果是这样,提供URL似乎不切实际,因为用户不需要进行验证,但是在Web路由中创建一条路径来运行Laravel Scheduler似乎有点安全漏洞。

You can create a executable cron.php in your public folder of laravel. 您可以在cron.php中创建可执行文件cron.php。

exec("/usr/bin/php7.2-cli -f /homepages/x/xxxxx/htdocs/artisan schedule:run 2>&1", $out, $result);

if(!empty($out)) {
    echo implode("<br>\n\n", $out);
}

Now you can call http://example.org/cron.php 现在您可以致电http://example.org/cron.php

Of course you can put the cron.php in a protected directory so no one can call the cron.php . 当然,您可以将cron.php放在受保护的目录中,这样没人可以调用cron.php

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

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