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

Laravel 5.6.13 Dusk定时任务(Cron)执行失败求助

Hey there, let's dig into why your Laravel Dusk cron job isn't running even though manual execution works. I've run into this exact scenario before, so here are the most likely fixes to try:

1. First, confirm your Laravel scheduler cron entry exists

This is the most common oversight—if you haven't added the core Laravel scheduler trigger to your server's crontab, none of your scheduled commands will run. Check if you have this line in your crontab (run crontab -e to edit):

* * * * * cd /absolute/path/to/your/laravel/project && php artisan schedule:run >> /dev/null 2>&1

Replace /absolute/path/to/your/laravel/project with the real root directory of your app.

2. Fix environment & path mismatches

Cron runs in a stripped-down environment, so it doesn't inherit your user's PATH or environment variables. Fix this in your schedule definition:

protected function schedule(Schedule $schedule) {
    $schedule->command('dusk tests/Browser/ProductComment.php')
        ->everyFifteenMinutes()
        ->workingDirectory('/absolute/path/to/your/laravel/project') // Critical: sets the correct working directory
        ->environment('production') // Match your app's environment
        ->sendOutputTo(storage_path('logs/dusk_cron.log')); // Force logs to debug
}

You can also explicitly specify the PHP binary path if needed (e.g., /usr/bin/php instead of just php in the crontab entry).

3. Fix permissions & browser driver issues

Dusk needs proper permissions to run headless Chrome/Chromium, and Cron runs under a different user (often www-data or your server's default user):

  • Make sure the Cron user has read/write access to your app's storage/ and bootstrap/cache/ directories.
  • Ensure your ChromeDriver is executable:
    chmod +x vendor/laravel/dusk/bin/chromedriver-linux # Adjust for your OS (mac/windows)
    
  • Update your Dusk driver configuration in tests/DuskTestCase.php to include critical headless flags (Cron often requires these):
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--headless=new', // Use the updated headless mode for modern Chrome
            '--no-sandbox', // Required for Cron environments without a full user session
            '--disable-dev-shm-usage',
            '--disable-gpu',
        ]);
    
        return RemoteWebDriver::create(
            'http://localhost:9515',
            DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY,
                $options
            )
        );
    }
    

Also verify that Chrome/Chromium is installed on your server and that the ChromeDriver version matches your browser version.

4. Force detailed logging

Since your Laravel logs aren't showing anything, capture Cron's raw output directly. Modify your crontab entry to log everything:

* * * * * cd /absolute/path/to/your/project && php artisan schedule:run >> /var/log/laravel_schedule.log 2>&1

Or log the Dusk command specifically:

*/15 * * * * cd /absolute/path/to/your/project && php artisan dusk tests/Browser/ProductComment.php >> /var/log/dusk_specific.log 2>&1

This will capture errors that Laravel's logs might miss, like missing dependencies or permission denials.

5. Verify Cron service is running

Double-check that the Cron daemon is active on your server:

  • For Debian/Ubuntu: sudo systemctl status cron
  • For CentOS/RHEL: sudo systemctl status crond
    If it's not running, start it with sudo systemctl start cron and enable auto-start with sudo systemctl enable cron.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:38:37