在 Debian 系统中,为 Laravel 设置定时任务通常是通过 Linux 的 cron + Laravel 的 Task Scheduling 来实现的。下面是完整、常用的做法。
Laravel 本身不自己跑定时,而是:
只用一个 cron 定时调用
php artisan schedule:run
然后由 Laravel 决定哪些任务该执行
routes/console.php 或 app/Console/Kernel.php 中定义任务方式一(推荐,简单):routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::call(function () {
// 你的逻辑
})->everyMinute();
方式二:app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
}
常见频率:
->everyMinute()->everyFiveMinutes()->hourly()->daily()->weekly()crontab -e
如果是 www-data(Web 用户),建议:
sudo crontab -u www-data -e
* * * * * cd /var/www/laravel && php artisan schedule:run >> /dev/null 2>&1
说明:
* * * * *:每分钟执行cd /var/www/laravel:你的 Laravel 目录php artisan schedule:run:Laravel 调度器>> /dev/null 2>&1:不输出日志(可选)✅ 只需这一条 cron
用绝对路径:
* * * * * cd /var/www/laravel && /usr/bin/php artisan schedule:run
查看 php 路径:
which php
确保:
storage/ 可写开启日志:
Schedule::command('your:command')
->daily()
->appendOutputTo(storage_path('logs/schedule.log'));
sudo crontab -u www-data -e
* * * * * /usr/bin/php /var/www/laravel/artisan schedule:run >> /var/www/laravel/storage/logs/cron.log 2>&1
Debian 上 Laravel 定时任务 = 1 条 cron + Laravel Schedule
如果你愿意,可以告诉我:
我可以帮你写完全适配你服务器的配置。