# Create custom CLI command

Using hostware modules you can easiliy create cli commands which can be executed with `php artisan` or as a scheduled task. This also has the benefit that debugging scheduled tasks is as easy as executing the command you created.

{% hint style="info" %}
hostware uses the default commands of the **Laravel Framework**. Take a look at the [official documentation](https://laravel.com/docs/10.x/artisan#command-structure)!
{% endhint %}

## Creating a command

A command is a class file which is stored in the directory /Console/COMMANDNAME.php and looks like the following:

{% code title="Console/CheckInstallingProducts.php" %}

```php
<?php

namespace custom\modules\DashservVps\Console;

class CheckInstallingProducts extends Command
{
    // The unique name of the command which will be used for execution
    protected $name = 'dashservvps:checkInstallingProducts';
    
    // A small description of what the command will do
    protected $description = 'Checks pending hosting products for installation';

    public function __construct()
    {
        parent::__construct();
    }

    // This function will execute once the command will be executed. You can perform anything inside!
    public function handle()
    {
        // YOUR CODE    
    }
}
```

{% endcode %}

The file and classname should be identical. You can now execute the command via the $name you have defined inside of it. Please use your module name as a prefix and after the : you can use the name of your class as name of the command.

## Register the command

To register the command of your module, please open the ServiceProvider class of your module in the directory /Providers/ (In this case /Providers/DashservVpsServiceProvider.php)

This file may look like the following per default:

{% code title="/Providers/DashservVpsServiceProvider.php" %}

```php
<?php

namespace custom\modules\DashservVps\Providers;

use App\Framework\Module\HwModuleServiceProvider;

class DashservVpsServiceProvider extends HwModuleServiceProvider {

}
```

{% endcode %}

To register the command, add the boot() function and the register the class of your command. The file should look like this afterwards:

{% code title="/Providers/DashservVpsServiceProvider.php" %}

```php
<?php

namespace custom\modules\DashservVps\Providers;

use App\Framework\Module\HwModuleServiceProvider;
use custom\modules\DashservVps\Console\CheckInstallingProducts;
use Illuminate\Console\Scheduling\Schedule;

class DashservVpsServiceProvider extends HwModuleServiceProvider {
	public function boot() {
		parent::boot();

		$this->commands([
			CheckInstallingProducts::class // <-- Add the class of your command here!
		]);
	}
}

```

{% endcode %}

## Executing the command

You can now execute the command in your hostware root directory calling `php artisan COMMANDNAME`. By just executing `php artisan` you get an overview of all registered comamnds.

In this case, you would need to execute php artisan `dashservvps:checkInstallingProducts` which would then execute the code inside of the handle() function.

## Execute as scheduled task

Learn here how to execute the command as a scheduled task in predefined intervals.
