You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Hostinger面板配置Laravel Cron Jobs定时任务遇阻求助

Hostinger上Laravel定时任务失效的解决方法

问题排查与修复步骤

1. 修正Bash脚本的命令错误

你的bash脚本第一个命令未带任何参数,执行后会直接输出Artisan命令列表,这就是你看到的输出结果。同时第二个命令未指定完整PHP路径,也未确保在项目根目录执行,正确的脚本写法如下:

cd /home/u482004401/domains/caviom.org/public_html && /usr/bin/php artisan schedule:run > /dev/null 2>&1
  • 先通过cd进入Laravel项目根目录,确保Artisan能正确加载项目环境
  • 使用完整PHP路径执行artisan schedule:run,避免环境变量问题
  • 保留输出重定向到/dev/null,防止Cron发送冗余通知邮件

2. 修正定时任务的逻辑错误

你的查询条件写反了:whereDate('subscription_expires_at', '>=', now())会匹配未到期的机构,而你需要将已到期的机构恢复为Free类型,应修改为:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        CharitableOrganization::whereDate('subscription_expires_at', '<=', now())
            ->update([
                'subscription' => 'Free',
                'subscribed_at' => null,
                'subscription_expires_at' => null
            ]);
    })->everyMinute();
}

3. Hostinger Cron任务的正确配置

在Hostinger的Cron任务面板中:

  • 执行周期选择每分钟(* * * * *)
  • 命令行填写bash脚本的完整路径,例如:/home/u482004401/domains/caviom.org/public_html/laravel-schedule.sh
  • 给bash脚本添加可执行权限:执行chmod +x /home/u482004401/domains/caviom.org/public_html/laravel-schedule.sh

验证方法

可以暂时修改脚本,将输出重定向到日志文件,方便排查问题:

cd /home/u482004401/domains/caviom.org/public_html && /usr/bin/php artisan schedule:run >> /home/u482004401/domains/caviom.org/public_html/cron.log 2>&1

等待1分钟后查看cron.log,确认是否有任务执行记录或错误信息。


内容的提问来源于stack exchange,提问作者Joseph Miguel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:45:37