I want to run simple bash scripts using laravel command class, but I have not found error.
If I go to /var/www/mysite/storage/app/scripts and run script from there in command line, then everything is OK.
$ sh remove-spaces.sh
What is wrong in my Laravel code?
lowercase.sh
for file in /var/www/mysite/storage/app/img/*;
do mv "$file" "`echo $file | tr '[A-Z]' '[a-z]'`";
done
remove-spaces.sh
for file in /var/www/mysite/storage/app/img/*;
do mv "$file" `echo $file | tr ' ' '-'`;
done
RenamePicturesCommand
namespace App\Console\Commands\Pictures;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class RenamePicturesCommand extends Command
{
protected $removeSpaces;
protected $lowercase;
protected $signature = 'pictures:rename';
public function __construct()
{
parent::__construct();
$this->removeSpaces = new Process(['sh /var/www/mysite/storage/app/scripts/remove-spaces.sh']);
$this->lowercase = new Process(['sh /var/www/mysite/storage/app/scripts/lowercase.sh']);
}
public function handle()
{
$this->removeSpaces->run();
if (!$this->removeSpaces->isSuccessful()) {
throw new ProcessFailedException($this->removeSpaces);
}
echo $this->removeSpaces->getOutput();
$this->lowercase->run();
if (!$this->lowercase->isSuccessful()) {
throw new ProcessFailedException($this->lowercase);
}
echo $this->lowercase->getOutput();
}
}
error output
http@0bb690b74597:/var/www/mysite$ php artisan pictures:rename
Symfony\Component\Process\Exception\ProcessFailedException
The command "'sh /var/www/mysite/storage/app/scripts/remove-spaces.sh'" failed.
Exit Code: 127(Command not found)
Working directory: /var/www/mysite
Output:
================
Error Output:
================
sh: 1: exec: sh /var/www/mysite/storage/app/scripts/remove-spaces.sh: not found
at app/Console/Commands/Pictures/RenamePicturesCommand.php:59
55▕ // execute command
56▕ $this->removeSpaces->run();
57▕ // executes after the command finishes
58▕ if (!$this->removeSpaces->isSuccessful()) {
➜ 59▕ throw new ProcessFailedException($this->removeSpaces);
60▕ }
61▕ echo $this->removeSpaces->getOutput();
62▕
63▕
+13 vendor frames
14 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
exec("sh your_script.sh", $output); $this->comment( implode( PHP_EOL, $output ) );