5

I want to get an artisan command to run a bash script when executed.

So I've created an artisan command using the following

php artisan make:command backupList --command=backup:list

And here is backupList.php

<?php

namespace App\Console\Commands;

require_once __DIR__ . '/vendor/autoload.php'; 

use Illuminate\Console\Command;


class backupDB extends Command
{

protected $signature = 'backup:list {name}';

protected $description = 'Database backup tool';



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



public function handle()
{
    $this->exec('ls -la');
}
}

In handle() exec and shell_exec don't seem to work, are there any alternatives to get the artisan command to run bash in shell?

2 Answers 2

18

Rather than use:

$this->exec('ls -la');

You can simply do the following:

// execute command
exec("ls -la", $output);

// print output from command
$this->comment( implode( PHP_EOL, $output ) );
Sign up to request clarification or add additional context in comments.

1 Comment

That did it :D you are awesome. Thank you!
6

Since Laravel uses Symphony in core. You can use Symphony Components that are already implemented to Laravel. For this situation you can use this one

http://symfony.com/doc/current/components/process.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.