Run another Laravels' Artisan as a Subprocess from Laravel's Artisan

Updated: Feb 14, 2025

Run another Laravels' Artisan as a Subprocess from Laravel's Artisan

In Laravel, Artisan is a powerful command-line interface tool that comes with various built-in commands for managing and building your Laravel application. Sometimes, you might need to run another Laravel Artisan command as a subprocess from within another Laravel Artisan command. This can be achieved using the Process class from Laravel's Illuminate\Support\Facades namespace.

Here's a step-by-step guide on how to run another Laravel Artisan command as a subprocess:

  1. Create a new command class for the parent command that will run the child command. Let's call it RunChildCommand. Create a new file app/Console/Commands/RunChildCommand.php with the following content:
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Process;

class RunChildCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run:child {command : The child command to run}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run a child Laravel Artisan command as a subprocess';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $command = $this->argument('command');

        // Run the child command as a subprocess using Laravel's Process class
        Process::run('php artisan ' . $command);

        // Optionally, you can return the exit code of the child process
        return Process::lastOutputOnError() ? Process::exitCode() : 0;
    }
}
  1. Register the new command class in the app/Console/Kernel.php file by adding it to the $commands array:
protected $commands = [
    // ...
    App\Console\Commands\RunChildCommand::class,
];
  1. Now you can use the RunChildCommand to run another Laravel Artisan command as a subprocess. For example, you can create a new Artisan command ClearCacheCommand that clears the Laravel cache:
namespace App\Console\Commands;

use Illuminate\Console\Command;

class ClearCacheCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cache:clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clear the Laravel cache';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('Clearing Laravel cache...');

        // Clear the Laravel cache using the Artisan command
        Artisan::call('cache:clear');

        $this->info('Laravel cache cleared.');
    }
}
  1. Register the new ClearCacheCommand in the app/Console/Kernel.php file:
protected $commands = [
    // ...
    App\Console\Commands\ClearCacheCommand::class,
];
  1. Now you can use the RunChildCommand to run the ClearCacheCommand as a subprocess:
php artisan run:child cache:clear

This will run the ClearCacheCommand as a subprocess of the RunChildCommand. The exit code of the ClearCacheCommand will be returned by the RunChildCommand.

Note that running commands as subprocesses can have some limitations and potential issues, such as not being able to capture the output of the child command directly in the parent command. You might need to use other methods, such as pipes or streams, to capture the output of the child command if necessary.