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:
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.
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).
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/andbootstrap/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.phpto 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.
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.
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 withsudo systemctl start cronand enable auto-start withsudo systemctl enable cron.
内容的提问来源于stack exchange,提问作者lolalola

