PhilMarc PhilMarc - 2 months ago 58
PHP Question

Artisan::call() outside the Laravel framework

I want to create a cron job for Laravel 5.2

My shared host (on OVH), only allows me to point to the full path of a file, and I am not able to use the recommended Cron entry from Laravel's docs, ie :

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Therefore, I have to call the Artisan command from a .php file, outside of the Laravel framework.

Here is what my
file looks like so far:


require __DIR__.'/../bootstrap/autoload.php';

use Illuminate\Support\Facades\Artisan;


being my command for regenerating thumbnails inside my app.

When accessing cron.php through my browser (testing on local XAMPP), the following error occurs:

Fatal error: Uncaught RuntimeException: A facade root has not been set. in

Stack trace:
#0 C:\xampp\htdocs\site\public\cron.php(7): Illuminate\Support\Facades\Facade::__callStatic('call', Array)
#1 {main} thrown in C:\xampp\htdocs\site\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 210

I have also tried to boot the app, but it doesn't make any differences

$app = require_once __DIR__.'/../bootstrap/app.php';

To avoid using the Artisan Facade, I tried calling the underlying Kernel Class directly:

use Illuminate\Contracts\Console\Kernel;

$kernel = new Kernel;

But this returns:

Uncaught Error: Cannot instantiate interface Illuminate\Contracts\Console\Kernel

EDIT: Here is a screenshot of OVH cron interface. The cron task is customized by OVH and only allows to point to the fullpath uri of a file - which file would execute my artisan command-. My question is, what should I put in this file, and should it be a PHP file, or a CMD?

OVH cron interface


What you want to do is run a specific Artisan command from within a script.

You can achieve this by copying artisan.php and forcing the input to what you want:

#!/usr/bin/env php

require __DIR__.'/bootstrap/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
    $input = new Symfony\Component\Console\Input\ArrayInput(['command' => 'refresh']),
    new Symfony\Component\Console\Output\ConsoleOutput

$kernel->terminate($input, $status);


If you compare this script to artisan.php, you'll see that I've just forced the input passed to $kernel->handle() method. It does not read the input from the CLI anymore, it takes these arguments, as an array. See Symfony Console Component documentation for more details.

If you need to pass arguments to your script, just set the input accordingly:

$input = new Symfony\Component\Console\Input\ArrayInput([
    'command' => 'refresh',
    'arg_foo' => 'foo',
    '--option_bar' => 42

$status = $kernel->handle(
    new Symfony\Component\Console\Output\ConsoleOutput

Now, you can put this script where you want, it does not need to be accessible through web via a browser (it should not, by the way).

If you put it at the root of your hosting at OVH, I mean NOT in www, you just have to fill the form very simply:

OVH Shared Hosting Cronjob - Step 1

If you want your script to be accessible through the web (which is not recommanded for obvious security reasons, but still), place it in your www directory, change the paths to bootstrap/autoload.php and bootstrap/app.php and give your script a name that is not easy to guess.

In the form in the OVH manager, don't forget to add www/ at the beginning of the path of the script.

There is no need to specify php script_name, since the manager handles it for you, when you choose PHP version. Just type the path of the script PHP will execute.