无SSH权限的共享主机上正确配置Magento Cron的方法
Got it, let's tackle this properly since your site is going live soon. Your current workaround gets the job done but isn't aligned with Magento 2's official best practices—plus it's wasting resources with unnecessary cache cleans and duplicate indexer runs. Here's the standardized, host-friendly solution for cPanel:
First, Why Your Current Setup Isn't Ideal
cache:cleanevery 15 minutes: This is overkill. Magento automatically invalidates cache when needed, and frequent cleaning will slow down your site by forcing repeated cache rebuilds.indexer:reindexevery 5 minutes: If your indexers are set to Update on Schedule (the recommended mode for live sites), Magento's core cron will handle reindexing automatically. Running this manually creates redundant processes and can cause lock conflicts.
Step-by-Step Correct Cron Configuration
Hostinger's cPanel lets you set up the three official Magento 2 cron tasks—these cover all core scheduled jobs (indexing, order processing, emails, module tasks, etc.).
1. Find Your Host's PHP Executable Path
Shared hosts often require a full PHP path instead of just php (which might point to a different PHP version than your site uses). To get it:
- Create a
phpinfo.phpfile in yourpublic_htmlfolder with this code:<?php phpinfo(); ?> - Visit the file in your browser, then search for "Executable Path" or "Configuration File (php.ini) Path" to get the full path (e.g.,
/opt/cpanel/ea-php71/bin/php—Magento 2.1.17 requires PHP 7.0-7.1).
2. Add the Three Official Cron Tasks
In cPanel's Cron Jobs section, add these three entries one by one, replacing /path/to/php with the full path you found:
Core Magento Cron Task (Runs every minute)
Handles all scheduled core and module tasks, including index reindexing (when set to Update on Schedule):
* * * * * /path/to/php public_html/bin/magento cron:run | grep -v "Ran jobs by schedule" >> public_html/var/log/magento.cron.log
The grep -v part filters out repetitive success messages to keep your log clean.
Update Cron Task (Runs every minute)
Manages Magento system updates and patch checks:
* * * * * /path/to/php public_html/update/cron.php >> public_html/var/log/update.cron.log
Setup Cron Task (Runs every minute)
Handles installation-related tasks like module setup and database migrations:
* * * * * /path/to/php public_html/bin/magento setup:cron:run >> public_html/var/log/setup.cron.log
3. Clean Up Your Old Tasks
Delete your existing three cron entries (cron:run, cache:clean, indexer:reindex) to avoid conflicts and resource waste.
4. Configure Indexers for Automatic Reindexing
In your Magento admin panel:
- Go to Stores > Settings > Configuration > Advanced > System > Index Management
- Set all indexers to Update on Schedule (this tells Magento's cron to handle reindexing automatically)
- Click Save Config
5. Verify It's Working
- Check the log files in
public_html/var/log/(magento.cron.log, update.cron.log, setup.cron.log) after 1-2 minutes—you should see entries showing tasks running successfully. - In the admin, go to System > Cron Scheduler > Scheduled Jobs to confirm tasks are being scheduled and executed.
Key Notes for Hostinger Shared Hosting
- Hostinger allows cron runs every minute, which is exactly what Magento needs (Magento's internal scheduler handles task frequency from there).
- Ensure
public_html/bin/magentohas execution permissions (set to755via cPanel's File Manager if needed). - If you run into path issues, modify the cron command to first navigate to your public_html folder:
* * * * * cd public_html && /path/to/php bin/magento cron:run | grep -v "Ran jobs by schedule" >> var/log/magento.cron.log
内容的提问来源于stack exchange,提问作者Gert

