0

In laravel (v9) project i try to launch some bash command, like this

   <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Carbon\Carbon;

class SaveLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'eraselog';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Save current log and erase old log';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $copy = new Process(['cp storage/logs/laravel.log storage/logs/laravel_new.log']);

        $erase = new Process([' > storage/logs/laravel.log']);

        $copy->run();
        $erase->run();
    }
}

The problem: Commands doesn't work. Maybe this ins´t the proper way to launch this commands. So, anybody shows what's the right way?

1
  • WHAT IS "Commands doesn't work"? Commented Feb 16, 2022 at 16:27

1 Answer 1

1

You can use the Laravel File Storage commands:

Storage::disk('logs')->copy('laravel.log', 'laravel_new.log');

Storage::disk('logs')->delete('laravel.log');

You need add a new "disk" into the "config/filesystems.php":

    'disks' => [
       'logs' => [
          'driver' => 'logs',
          'root' => storage_path('logs'),
       ],
    ],

Laravel Docs: https://laravel.com/docs/9.x/filesystem#copying-moving-files

Sign up to request clarification or add additional context in comments.

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.