Laravel Task Scheduling: Managing Task Output

December 7, 2016 —John Koster

Laravel's task scheduling system makes it simple to manage the output from your tasks, if any. Output from task processes can be appended to a file or emailed to any given email address (provided that email services have been previously configured). The following table lists each of the various output management methods that are defined on the Illuminate\Console\Scheduling\Event base class. The following sections will examine each of the methods in further detail.

Method Description
sendOutputTo($location, $append = false) Send the task output to the given file location. If $append is set to true, the output will be appended at the end of the file.
appendOutputTo($location) Send the task output to the given file location. This is a helper method that calls sendOutputTo with a value of true supplied to the $append parameter.
emailOutputTo($addresses, $onlyIfOutputExists = false) E-mails the results of the task to the given email addresses. This method will always email the results even if no output has been generated (this can be changed by supplying a true value as the argument for the $onlyIfOutputExists parameter).
emailWrittenOutputTo($addresses) Only E-mails generated output to the provided addresses. This method is a helper method that calls emailOutToj with a value of true supplied to the $onlyIfOutputExists parameter.

It is important to note that the helper methods for managing task output are only available when scheduling tasks using the command or exec methods (the command method is a utility wrapper for Artisan commands built on top of the exec method). This is because the scheduling system does not directly manage the output of a task, but rather adds the output control instructions to the generated command that is issued to the environment operating system.

#Sending Output to a File

Sending output to a file is the base of task output management. The main method to accomplish this is the sendOutputTo($location, $append = false) method. It accepts the file $location as the argument and can be configured to either overwrite the existing file contents or append to the file by supplying a truth value argument to the $append parameter (by default the sendOutputTo method will overwrite the file contents). The following examples will assume that the Inspire command has been registered in the console kernel.

1<?php
2 
3// ...
4 
5protected function schedule(Schedule $schedule)
6{
7 $schedule->command('inspire')
8 ->everyMinute()
9 ->appendOutputTo(storage_path('logs/inspire.log'));
10}
11 
12// ...

The previous example would run the inspire Artisan command every minute and append the output to the inspire.log file within the application's storage log directory. After the command has been executed a few times, the log file might contain something similar to the following output (the exact output will differ as the inspire command selects messages at random):

1
2Simplicity is an acquired taste. - Katharine Gerould
3
4
5Simplicity is an acquired taste. - Katharine Gerould
6
7
8Smile, breathe, and go slowly. - Thich Nhat Hanh
9
Removing ANSI Color Characters

By default, the output of any Symfony-based command that uses the output helpers might contain ANSI color sequences. These can be disabled when sending the output to files or emails by calling the commands with the --no-ansi option. In the previous code example the command that would have to be ran is inspire --no-ansi.

#E-mailing Output

The output of tasks can also be emailed after the task has completed. Emailing output can be accomplished by either using the emailOutputTo and emailWrittenOutputTo methods. Each of these methods require that the output to have been previously saved to a file location using either the sendOutputTo or sendWrittenOutputTo methods.

The emailOutputTo method accepts either a single E-mail address, or an array of E-mail addresses as the argument for the $addresses parameter. Some tasks do not produce any output but will still cause an email to be sent. This can be changed by supplying a truth value as the argument for the $onlyIfOutputExists parameter. By default, the emailOutputTo method will always send an E-mail, even if the task does not generate any output. The emailWrittenOutputTo method is a helper method that internally makes a call to the emailOutputTo method. The emailWrittenOutputTo method will not send an E-mail when there is no task output.

The following examples demonstrate how to use the E-mail methods. It is important to note that the sendOutputTo (or any of the other file-based output management methods) method has been called first:

1<?php
2 
3// ...
4 
5protected function schedule(Schedule $schedule)
6{
7 // Sending output to a single address.
8 $schedule->command('command:name --options')
9 ->daily()
10 ->appendOutputTo($fileLocation)
11 ->emailOutputTo('example@example.com');
12 
13 // Sending output to multiple addresses.
14 $schedule->command('command:name --options')
15 ->daily()
16 ->appendOutputTo($fileLocation)
17 ->emailOutputTo([
18 'example@example.com',
19 'example_2@example.com'
20 ]);
21 
22 // Only send an E-mail if there is actually output.
23 $schedule->command('command:name --options')
24 ->daily()
25 ->appendOutputTo($fileLocation)
26 ->emailWrittenOutputTo('example@example.com');
27 
28 // Or...
29 $schedule->command('command:name --options')
30 ->daily()
31 ->appendOutputTo($fileLocation)
32 ->emailOutputTo('example@example.com', true);
33}
34 
35// ...

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.