Cron Setup
Configure scheduled tasks for automated order sync, inventory updates, and tracking synchronization.
Overview
GSM Middleware uses server cron jobs (not WordPress cron) for reliable, scheduled operations:
- Order Sync: Fetch new orders from e-commerce platforms
- Inventory Sync: Update product quantities from Business Central
- Tracking Sync: Post tracking numbers to e-commerce platforms
- BC Insert: Export orders to Business Central
- BC Verify: Verify orders created in Business Central
Prerequisites
- Shell/SSH access to server
- Permission to edit crontab
- PHP CLI installed
- Plugin installed and activated
Cron Scripts Location
/path/to/wordpress/wp-content/plugins/gsm-middleware/cron/
├── order-sync.php
├── inventory-sync.php
├── tracking-sync.php
├── bc-insert-items.php
└── bc-verify-items.php
Basic Cron Setup
Edit Crontab
# Open crontab editor
crontab -e
Add Cron Jobs
Recommended schedule:
# Order Sync - Every hour at :00
0 * * * * /usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/order-sync.php >> /var/log/gsm-middleware/order-sync.log 2>&1
# Inventory Sync - Every 30 minutes
*/30 * * * * /usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/inventory-sync.php >> /var/log/gsm-middleware/inventory-sync.log 2>&1
# Tracking Sync - Every 30 minutes
*/30 * * * * /usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/tracking-sync.php >> /var/log/gsm-middleware/tracking-sync.log 2>&1
# BC Insert - Every 15 minutes
*/15 * * * * /usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/bc-insert-items.php >> /var/log/gsm-middleware/bc-insert.log 2>&1
# BC Verify - Every 15 minutes (offset by 5 minutes)
5,20,35,50 * * * * /usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/bc-verify-items.php >> /var/log/gsm-middleware/bc-verify.log 2>&1
Save and Exit
- vi/vim: Press
Esc, type:wq, pressEnter - nano: Press
Ctrl+X, pressY, pressEnter
Cron Schedules Explained
Order Sync (Hourly)
0 * * * *
- Runs: Every hour at :00 (1:00, 2:00, 3:00...)
- Duration: 2-5 minutes typically
- Purpose: Fetch new orders from platforms
Why hourly?
- Balances freshness with server load
- Allows time for BC export to complete
- Prevents overwhelming external APIs
Inventory Sync (Every 30 Minutes)
*/30 * * * *
- Runs: :00 and :30 of every hour
- Duration: 5-10 minutes typically
- Purpose: Update product quantities
Why every 30 minutes?
- Keeps inventory reasonably current
- Prevents overselling
- BC inventory doesn't change instantly
Tracking Sync (Every 30 Minutes)
*/30 * * * *
- Runs: :00 and :30 of every hour
- Duration: 1-3 minutes typically
- Purpose: Post tracking numbers to platforms
BC Insert (Every 15 Minutes)
*/15 * * * *
- Runs: :00, :15, :30, :45 of every hour
- Duration: 1-2 minutes typically
- Purpose: Send orders to Business Central
Why every 15 minutes?
- Quick turnaround for order fulfillment
- Prevents large backlogs
- Manageable batch sizes
BC Verify (Every 15 Minutes, Offset)
5,20,35,50 * * * *
- Runs: :05, :20, :35, :50 of every hour
- Duration: 30-60 seconds typically
- Purpose: Confirm orders created in BC
Why offset?
- Runs after BC Insert has time to complete
- Prevents conflicts
- Ensures orders have time to appear in BC
Advanced Schedules
High-Volume Configuration
For sites with >1000 orders/day:
# Order Sync - Every 15 minutes
*/15 * * * * php cron/order-sync.php
# Inventory Sync - Every 15 minutes
*/15 * * * * php cron/inventory-sync.php
# BC Insert - Every 5 minutes
*/5 * * * * php cron/bc-insert-items.php
# BC Verify - Every 5 minutes (offset)
2,7,12,17,22,27,32,37,42,47,52,57 * * * * php cron/bc-verify-items.php
Low-Volume Configuration
For sites with <100 orders/day:
# Order Sync - Every 2 hours
0 */2 * * * php cron/order-sync.php
# Inventory Sync - Every hour
0 * * * * php cron/inventory-sync.php
# BC Insert - Every 30 minutes
*/30 * * * * php cron/bc-insert-items.php
# BC Verify - Every 30 minutes
15,45 * * * * php cron/bc-verify-items.php
Log Management
Create Log Directory
sudo mkdir -p /var/log/gsm-middleware
sudo chown www-data:www-data /var/log/gsm-middleware
sudo chmod 755 /var/log/gsm-middleware
Rotate Logs
Create logrotate configuration:
sudo nano /etc/logrotate.d/gsm-middleware
Add configuration:
/var/log/gsm-middleware/*.log {
daily
rotate 30
compress
delaycompress
notifempty
create 0640 www-data www-data
sharedscripts
}
Monitoring Cron Jobs
Check if Cron is Running
# View crontab
crontab -l
# Check cron service
sudo systemctl status cron
# Check cron logs
sudo tail -f /var/log/syslog | grep CRON
Monitor Execution
Real-time monitoring:
# Watch all logs
tail -f /var/log/gsm-middleware/*.log
# Watch specific operation
tail -f /var/log/gsm-middleware/order-sync.log
Check last run:
SELECT name,
order_last_run,
inventory_last_run,
tracking_last_run
FROM rm_sites;
Troubleshooting
Cron Not Running
Check cron service:
sudo systemctl status cron
sudo systemctl restart cron
Check syntax:
# Test PHP script manually
/usr/bin/php /path/to/cron/order-sync.php
Check permissions:
ls -la /path/to/cron/
# Should be executable
Jobs Running But Failing
Check logs:
grep ERROR /var/log/gsm-middleware/*.log
Common issues:
- PHP memory limit
- Execution timeout
- Database connection
- API credentials
Multiple Instances Running
Prevent overlapping:
Add lock file check to scripts or use flock:
*/15 * * * * /usr/bin/flock -n /tmp/order-sync.lock /usr/bin/php /path/to/cron/order-sync.php
Alternative: systemd Timers
Create Service File
sudo nano /etc/systemd/system/gsm-order-sync.service
[Unit]
Description=GSM Middleware Order Sync
After=network.target
[Service]
Type=oneshot
User=www-data
ExecStart=/usr/bin/php /var/www/html/wp-content/plugins/gsm-middleware/cron/order-sync.php
StandardOutput=append:/var/log/gsm-middleware/order-sync.log
StandardError=append:/var/log/gsm-middleware/order-sync.log
Create Timer File
sudo nano /etc/systemd/system/gsm-order-sync.timer
[Unit]
Description=GSM Middleware Order Sync Timer
Requires=gsm-order-sync.service
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
Enable Timer
sudo systemctl daemon-reload
sudo systemctl enable gsm-order-sync.timer
sudo systemctl start gsm-order-sync.timer
Per-Site Cron Jobs
Run for Specific Site
# Sync only site ID 5
SITE_ID=5 php cron/order-sync.php
Parallel Processing
# Multiple sites simultaneously
*/15 * * * * SITE_ID=5 php cron/order-sync.php &
*/15 * * * * SITE_ID=10 php cron/order-sync.php &
*/15 * * * * SITE_ID=15 php cron/order-sync.php &
Next Steps
- First Site Setup - Configure your first site
- Operations Guide - Detailed cron documentation
- Monitoring - Monitor cron execution