Laravel: Is it possible to run the cron job daily automatically?

✔ Recommended Answer

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

Source: stackoverflow.com

Answered By: Atena Dadkhah

Method #2

Yes, in Laravel you can schedule a cron job to run daily automatically using Laravel's built-in task scheduling functionality.

To schedule a task to run daily, you can use the daily() method in your App\Console\Kernel class. Here's an example:

php
protected function schedule(Schedule $schedule) { $schedule->command('my-command')->daily(); }

In this example, we are scheduling a command named my-command to run daily. The daily() method takes an optional parameter that allows you to specify the time of day at which the task should run. For example, if you want the task to run at 3:30 AM every day, you can do:

php
protected function schedule(Schedule $schedule) { $schedule->command('my-command')->dailyAt('3:30'); }

This will run the my-command command at 3:30 AM every day.

Note that in order for the task scheduling to work, you must set up a cron job on your server that runs the php artisan schedule:run command every minute. This command checks your scheduled tasks and runs any that are due to be executed. You can set up the cron job using your server's built-in scheduling system (e.g. crontab on Linux).

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755