Trupti
Trupti

Reputation: 973

How to run artisan command in bash script outside laravel?

I am using two frameworks i.e. Laravel and Symfony for two applications which are interlinked with each other. Both are having bash script.

Now, I want to write 'php artisan down' command in Symfony's bash script so that if I merge code to Symfony then both the applications gets down.

How can I write Laravel artisan command in Symfony framework?

Upvotes: 0

Views: 2739

Answers (2)

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

You can do this in your Symfony project using Symfony Console:

//in your symfony project
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;


public function processCmd() {
    $process = new Process('php /absolute/path/to/project/artisan down');
    $process->run();
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }
    echo $process->getOutput();
}

Update:

Laravel also uses the same symfony's Console component! So if you want to run something in Laravel, you can use the same code above.

Example:

    //in your laravel project 
    use Symfony\Component\Process\Process;
    use Symfony\Component\Process\Exception\ProcessFailedException;
    
    
    public function processCmd() {
        $process = new Process('supervisorctl stop all');
        $process->run();
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }
        echo $process->getOutput();
    }

P.S:

You can pass an array if you want to run multiple commands:

 $process = new Process(array('ls', '-lsa'));

Upvotes: 1

namelivia
namelivia

Reputation: 2735

It should work like any other command:

#!/bin/bash
php /full/project/path/artisan down

Just write the full path.

Upvotes: 1

Related Questions