skip to Main Content

I just learned earlier how to create a cron job, now I want to execute the command daily automatically.

In my kernel file, I declared it everyMinute but when I check the path it didnt backup the file

DbBackupDaily

<?php

namespace AppConsoleCommands;

use IlluminateConsoleCommand;

class DbBackupDaily extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:backupdaily';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Database Backup Daily';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $file_name = "backup_".strtotime(now()).".sql";
        $command = "D:/wamp64/bin/mysql/mysql5.7.31/bin/mysqldump.exe --user=".env('DB_USERNAME')." --password=".env('DB_PASSWORD')." --host=".env('DB_HOST')." --port=".env('DB_PORT')." ".env('DB_DATABASE')." > ".storage_path()."/app/public/backup/".$file_name ;
        exec($command);

    }
}

Kernel

protected $commands = [
        "AppConsoleCommandsDbBackupDaily",
        "AppConsoleCommandsDeleteDbBackupMonthly"
    ];

    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('db:backup')->everyMinute();
        $schedule->command('db:deletebackupmonthly')->monthly();

    }

Question: How do I execute the command daily automatically?

2

Answers


  1. You should run this command if you are in localhost:

    php artisan schedule:work
    

    Or if you are on a host, add this command to your cron jobs and set that to run every minute:

    /usr/local/bin/php /path-to-project/artisan schedule:run >> /dev/null 2>&1
    
    Login or Signup to reply.
  2. You must add your schedule to a cron job.

    In your panel set path/to/php artisan schedule:run for every minute and it will run the commands as you specified.

    If you are working locally for running schedules you can use php artisan schedule:work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search