Artisan CLI

Introduction

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

Usage

Listing All Available Commands

To view a list of all available Artisan commands, you may use the list command:

  1. php artisan list

Viewing The Help Screen For A Command

Every command also includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, simply precede the name of the command with help:

  1. php artisan help migrate

Specifying The Configuration Environment

You may specify the configuration environment that should be used while running a command using the —env switch:

  1. php artisan migrate --env=local

Displaying Your Current Laravel Version

You may also view the current version of your Laravel installation using the —version option:

  1. php artisan --version

Calling Commands Outside Of CLI

Sometimes you may wish to execute an Artisan command outside of the CLI. For example, you may wish to fire an Artisan command from an HTTP route. Just use the Artisan facade:

  1. Route::get('/foo', function()
  2. {
  3. $exitCode = Artisan::call('command:name', ['--option' => 'foo']);
  4. //
  5. });

You may even queue Artisan commands so they are processed in the background by your queue workers:

  1. Route::get('/foo', function()
  2. {
  3. Artisan::queue('command:name', ['--option' => 'foo']);
  4. //
  5. });

Scheduling Artisan Commands

In the past, developers have generated a Cron entry for each console command they wished to schedule. However, this is a headache. Your console schedule is no longer in source control, and you must SSH into your server to add the Cron entries. Let's make our lives easier. The Laravel command scheduler allows you to fluently and expressively define your command schedule within Laravel itself, and only a single Cron entry is needed on your server.

Your command schedule is stored in the app/Console/Kernel.php file. Within this class you will see a schedule method. To help you get started, a simple example is included with the method. You are free to add as many scheduled jobs as you wish to the Schedule object. The only Cron entry you need to add to your server is this:

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

This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled jobs and runs the jobs that are due. It couldn't be easier!

More Scheduling Examples

Let's look at a few more scheduling examples:

Scheduling Closures

  1. $schedule->call(function()
  2. {
  3. // Do some task...
  4. })->hourly();

Scheduling Terminal Commands

  1. $schedule->exec('composer self-update')->daily();

Manual Cron Expression

  1. $schedule->command('foo')->cron('* * * * *');

Frequent Jobs

  1. $schedule->command('foo')->everyFiveMinutes();
  2. $schedule->command('foo')->everyTenMinutes();
  3. $schedule->command('foo')->everyThirtyMinutes();

Daily Jobs

  1. $schedule->command('foo')->daily();

Daily Jobs At A Specific Time (24 Hour Time)

  1. $schedule->command('foo')->dailyAt('15:00');

Twice Daily Jobs

  1. $schedule->command('foo')->twiceDaily();

Job That Runs Every Weekday

  1. $schedule->command('foo')->weekdays();

Weekly Jobs

  1. $schedule->command('foo')->weekly();
  2. // Schedule weekly job for specific day (0-6) and time...
  3. $schedule->command('foo')->weeklyOn(1, '8:00');

Monthly Jobs

  1. $schedule->command('foo')->monthly();

Job That Runs On Specific Days

  1. $schedule->command('foo')->mondays();
  2. $schedule->command('foo')->tuesdays();
  3. $schedule->command('foo')->wednesdays();
  4. $schedule->command('foo')->thursdays();
  5. $schedule->command('foo')->fridays();
  6. $schedule->command('foo')->saturdays();
  7. $schedule->command('foo')->sundays();

Prevent Jobs From Overlapping

By default, scheduled jobs will be run even if the previous instance of the job is still running. To prevent this, you may use the withoutOverlapping method:

  1. $schedule->command('foo')->withoutOverlapping();

In this example, the foo command will be run every minute if it is not already running.

Limit The Environment The Jobs Should Run In

  1. $schedule->command('foo')->monthly()->environments('production');

Indicate The Job Should Run Even When Application Is In Maintenance Mode

  1. $schedule->command('foo')->monthly()->evenInMaintenanceMode();

Only Allow Job To Run When Callback Is True

  1. $schedule->command('foo')->monthly()->when(function()
  2. {
  3. return true;
  4. });

E-mail The Output Of A Scheduled Job

  1. $schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('[email protected]');

Note: You must send the output to a file before it can be mailed.

Send The Output Of The Scheduled Job To A Given Location

  1. $schedule->command('foo')->sendOutputTo($filePath);

Ping A Given URL After The Job Runs

  1. $schedule->command('foo')->thenPing($url);

Using the thenPing($url) feature requires the Guzzle HTTP library. You can add Guzzle 5 to your project by adding the following line to your composer.json file:

  1. "guzzlehttp/guzzle": "~5.0"