简体   繁体   中英

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 . It essentially grabs data and updates a few tables.

I followed a help article on 1&1 about creating 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.

This will require the PHP path and the path to the script that is to be run.

The issue is when I run that command I get the following.

No crontab for [username]

So, I then Googled and it seems some shared hosts don't allow you to use or even access the crontab.

On my shared hosting server the path to php-7.1-cli is as follows /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

Given this, for the cronjob I thought I'd be able to do something like this:

* * * * * /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.

在此处输入图片说明

Am I being prevented from using 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.

You can create a executable cron.php in your public folder of laravel.

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

Of course you can put the cron.php in a protected directory so no one can call the cron.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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