Cron Jobs in Magento 2
Cron jobs zijn scheduled taken die automatisch draaien. Magento gebruikt cron voor veel essentiële functies.
Wat draait via cron?
Essentiële taken
- Indexers (scheduled mode)
- Email queue
- Currency rates update
- Sitemap generation
- Catalog price rules
- Newsletter queue
- Automatic invoicing
- Log cleaning
Server cron configureren
Crontab bewerken
``bash
crontab -e -u www-data
`
Magento cron entries
`bash
* * * * * /usr/bin/php /path/to/magento/bin/magento cron:run >> /var/log/magento.cron.log 2>&1
`
Alle Magento crons
`bash
# Main cron - runs every minute
* * * * * /usr/bin/php /path/to/magento/bin/magento cron:run >> /var/log/magento.cron.log 2>&1
# Remove old cron tasks
*/5 * * * * /usr/bin/php /path/to/magento/bin/magento cron:remove >> /var/log/magento.cron.log 2>&1
`
Cron groepen
Standaard groepen
- default: Algemene taken
- index: Indexer taken
- catalog_event: Catalog events
- staging: Content staging (Adobe Commerce)
Configuratie per groep
Stores > Configuration > Advanced > System > Cron
Per groep:
- Schedule Generate Every: Hoe vaak schedule genereren
- Schedule Ahead for: Hoever vooruit schedulen
- Missed if Not Run Within: Timeout voor gemiste taken
- History Cleanup Every: Cleanup frequentie
- Success History Lifetime: Succesvolle taken bewaren
- Failure History Lifetime: Gefaalde taken bewaren
Cron status controleren
Via CLI
`bash
bin/magento cron:status
`
Via database
`sql
SELECT * FROM cron_schedule
ORDER BY scheduled_at DESC
LIMIT 50;
`
Status waarden
- pending: Wacht op uitvoering
- running: Momenteel actief
- success: Succesvol afgerond
- missed: Gemist (timeout)
- error: Gefaald
Cron handmatig uitvoeren
Alle scheduled crons
`bash
bin/magento cron:run
`
Specifieke groep
`bash
bin/magento cron:run --group=index
`
Specifieke taak
Niet standaard mogelijk. Gebruik:
`bash
bin/magento indexer:reindex catalog_product_price
`
Veelvoorkomende problemen
Cron draait niet
Checklist:
Is crontab correct ingesteld?
`bash
crontab -l -u www-data
`
Heeft PHP CLI de juiste configuratie?
`bash
php -v
php -i | grep memory_limit
`
Check cron log:
`bash
tail -f /var/log/magento.cron.log
`
Cron loopt vast
Symptoom: Taken blijven "running" status
Oplossing:
Check processen:
`bash
ps aux | grep magento
`
Kill hangende processen:
`bash
kill -9 [PID]
`
Reset cron schedule:
`sql
UPDATE cron_schedule SET status = 'error' WHERE status = 'running';
`
Cron table vol
Symptoom: Database groeit, performance daalt
Oplossing:
Cleanup handmatig:
`bash
bin/magento cron:remove
`
Verkort history lifetime in configuratie
Memory issues
Symptoom: Cron faalt met memory errors
Oplossing:
- Verhoog PHP CLI memory limit
- Controleer of taken niet parallel draaien
- Split grote taken
Cron voor indexers
Index mode
Indexers kunnen draaien:
- Update on Save: Direct na wijziging
- Update by Schedule: Via cron
Voordeel scheduled
Minder load tijdens admin werk. Bulk wijzigingen worden batch-verwerkt.
Instellen
`bash
bin/magento indexer:set-mode schedule
`
Nadeel
Data niet direct actueel. Wacht op volgende cron run.
Custom cron jobs
Crontab.xml
In je custom module:
`xml
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
0 2 * * *
`
Cron class
`php
namespace Vendor\Module\Cron;
class Cleanup
{
public function execute()
{
// Cron logic here
return $this;
}
}
`
Monitoring
Cron health check
Script om cron status te monitoren:
`bash
#!/bin/bash
LAST_RUN=$(mysql -u user -p db -e "SELECT MAX(executed_at) FROM cron_schedule WHERE status='success'")
# Alert als langer dan 5 minuten geleden
``
External monitoring
- New Relic cron monitoring
- Healthchecks.io
- Custom alerting
Performance tips
Niet te frequent
Elke minuut is vaak niet nodig. Check of taken echt zo vaak moeten draaien.
Spreide taken
Niet alle taken tegelijk om 00:00. Spreid over de dag.
Resource limieten
Zet limieten op cron processen om server overload te voorkomen.
Best practices
Logging
Zorg voor goede logging:
- Cron output naar logfile
- Monitor op errors
- Rotate logs
Documentatie
Documenteer welke crons draaien en waarom.
Testing
Test cron configuratie op staging voor naar productie.
Monitoring
Actieve monitoring van cron health:
- Alerting bij failures
- Dashboard voor status