本文介绍了Laravel 5-如何创建Artisan命令来执行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得一个工匠命令,以便在执行时运行bash脚本.
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
这是backupList.php
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');
}
}
在handle()中exec和shell_exec似乎不起作用,是否有其他选择可以使artisan命令在shell中运行bash?
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?
推荐答案
而不是使用:
$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 ) );
这篇关于Laravel 5-如何创建Artisan命令来执行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!