Skip to content

mb-cron Service

Scheduled task execution daemon for automated maintenance and housekeeping.

Overview

mb-cron manages all scheduled tasks in Mailborder:

  • Signature updates - Virus and spam definitions
  • Database maintenance - Cleanup and optimization
  • Backup automation - Scheduled backups
  • Report generation - Automated reports
  • Log rotation - Archive old logs
  • Cleanup tasks - Remove expired data

Runs as SystemD timer (every 5 minutes by default).

Architecture

Timer System

Uses SystemD timer instead of traditional cron:

mb-cron.timer (triggers every 5 min)
mb-cron.service (oneshot)
mb-cron script
Execute scheduled tasks

Task Types

  1. Frequent (every 5 min):
  2. Health checks
  3. Queue monitoring

  4. Hourly:

  5. Signature updates
  6. Statistics aggregation

  7. Daily:

  8. Database cleanup
  9. Log rotation
  10. Report generation

  11. Weekly:

  12. Database optimization
  13. Backup verification

  14. Monthly:

  15. Archive old data
  16. Full system maintenance

Configuration

Service Files

Timer: /etc/systemd/system/mb-cron.timer

[Unit]
Description=Mailborder Cron Timer
Requires=mb-cron.service

[Timer]
OnCalendar=*:0/5  # Every 5 minutes
Persistent=true

[Install]
WantedBy=timers.target

Service: /etc/systemd/system/mb-cron.service

[Unit]
Description=Mailborder Cron Service
After=network.target mb-rpcd.service

[Service]
Type=oneshot
User=mailborder
Group=mailborder
ExecStart=/usr/libexec/mailborder/php_enc/mb-cron run

Task Schedule

Configure schedules:

# Update frequency
sudo mb-config set cron.signature_update_schedule "hourly"
sudo mb-config set cron.backup_schedule "daily 02:00"
sudo mb-config set cron.report_schedule "daily 08:00"

# Cleanup
sudo mb-config set cron.log_cleanup_schedule "daily 03:00"
sudo mb-config set cron.quarantine_cleanup_schedule "daily 04:00"

Operations

Enable/Disable Timer

Enable:

sudo systemctl enable mb-cron.timer
sudo systemctl start mb-cron.timer

Disable:

sudo systemctl stop mb-cron.timer
sudo systemctl disable mb-cron.timer

Status:

sudo systemctl status mb-cron.timer
sudo systemctl list-timers | grep mb-cron

Manual Execution

Run all tasks:

sudo mb-cron run

Run specific task:

sudo mb-cron run --task signature_update
sudo mb-cron run --task database_cleanup
sudo mb-cron run --task backup

View Schedule

List configured tasks:

sudo mb-cron list

Example output:

Scheduled Tasks
===============

Frequent (every 5 min):
  - health_check
  - queue_monitor

Hourly (at :15):
  - signature_update
  - stats_aggregate

Daily:
  - database_cleanup (03:00)
  - log_rotation (03:30)
  - quarantine_cleanup (04:00)
  - backup (02:00)
  - reports (08:00)

Weekly (Sunday 01:00):
  - database_optimize
  - backup_verify

Monthly (1st, 02:00):
  - archive_old_data
  - full_maintenance

Scheduled Tasks

Signature Updates

What it does: - Updates ClamAV virus signatures - Updates Rspamd spam rules - Updates GeoIP database

Frequency: Hourly

Manual trigger:

sudo mb-update --all

Database Cleanup

What it does: - Deletes old audit logs (>90 days) - Deletes old email logs (>30 days) - Deletes old sessions - Removes expired entries

Frequency: Daily at 03:00

Manual trigger:

sudo mb-maintenance --database-cleanup

Log Rotation

What it does: - Rotates service logs - Compresses old logs - Deletes logs >30 days

Frequency: Daily at 03:30

Manual trigger:

sudo logrotate /etc/logrotate.d/mailborder

Quarantine Cleanup

What it does: - Deletes quarantine >30 days - Removes orphaned attachments - Updates statistics

Frequency: Daily at 04:00

Manual trigger:

sudo mb-quarantine-cleanup

Automated Backups

What it does: - Backs up configuration - Backs up database - Uploads to remote storage - Verifies backup integrity

Frequency: Daily at 02:00

Manual trigger:

sudo mb-backup --full

Report Generation

What it does: - Generates daily summary - Emails to administrators - Archives report

Frequency: Daily at 08:00

Manual trigger:

sudo mb-report --daily --email admin@example.com

Monitoring

Task History

View recent executions:

sudo mb-cron history

Example output:

Recent Task Executions
======================

2025-01-13 14:00:00  signature_update    SUCCESS  2.3s
2025-01-13 13:00:00  signature_update    SUCCESS  2.1s
2025-01-13 08:00:00  reports             SUCCESS  5.4s
2025-01-13 04:00:00  quarantine_cleanup  SUCCESS  12.1s
2025-01-13 03:30:00  log_rotation        SUCCESS  1.8s
2025-01-13 03:00:00  database_cleanup    SUCCESS  8.7s
2025-01-13 02:00:00  backup              SUCCESS  45.2s

Task Logs

View cron log:

sudo tail -f /var/log/mailborder/mb-cron.log

SystemD journal:

sudo journalctl -u mb-cron.service -n 50

Filter by task:

sudo grep "signature_update" /var/log/mailborder/mb-cron.log

Failed Tasks

Check for failures:

sudo mb-cron history --failed

Alert on failures:

# Automatically emails admin when task fails
sudo mb-config set cron.alert_on_failure true
sudo mb-config set cron.alert_email admin@example.com

Troubleshooting

Timer Not Running

Check timer status:

sudo systemctl status mb-cron.timer

Enable if disabled:

sudo systemctl enable mb-cron.timer
sudo systemctl start mb-cron.timer

Verify next run:

sudo systemctl list-timers | grep mb-cron

Task Failing

Check logs:

sudo journalctl -u mb-cron.service -n 100
sudo tail -f /var/log/mailborder/mb-cron.log

Run manually with debug:

sudo mb-cron run --task backup --debug

Common issues: - Disk full (backups fail) - Database connection timeout - Permission errors - Network issues (remote backups)

Task Taking Too Long

Check task duration:

sudo mb-cron history | grep -i slow

Optimize: - Database cleanup: Reduce retention - Backups: Exclude large data - Reports: Reduce date range

Best Practices

Scheduling

  1. Spread heavy tasks - Don't run all at 02:00
  2. Order dependencies - Cleanup before backup
  3. Off-peak times - Schedule during low traffic
  4. Monitor duration - Alert if task runs too long

Maintenance

  1. Review logs weekly - Check for failed tasks
  2. Adjust schedules - Based on actual needs
  3. Test manually - Before enabling automation
  4. Document changes - Track schedule modifications

Alerting

# Enable comprehensive alerts
sudo mb-config set cron.alert_on_failure true
sudo mb-config set cron.alert_on_slow true
sudo mb-config set cron.slow_threshold 300  # 5 minutes

See Also