Cron is the foundation of server automation. This article contains 15 ready-made jobs for common web development tasks: backups, cache cleanup, sending reports, and synchronization. Each job can be copied and adapted. To generate and validate schedules, use the Cron Generator on reChecker. The basic cron syntax is described in the cron jobs guide.
1. MySQL backup
A daily database dump at 3:00 with rotation of old copies:
0 3 * * * /usr/bin/mysqldump -u backup -p'password' --single-transaction --routines --triggers mydb | gzip > /backups/mydb_$(date +\%Y\%m\%d).sql.gzRotation—deleting dumps older than 7 days:
0 4 * * * find /backups -name "mydb_*.sql.gz" -mtime +7 -deleteIn crontab, the % character must be escaped: \%.
2. PostgreSQL backup
0 3 * * * /usr/bin/pg_dump -U postgres mydb | gzip > /backups/mydb_$(date +\%Y\%m\%d).sql.gzConfigured using .pgpass (a file with permissions 600):
0 3 * * * /usr/bin/pg_dump -U postgres -h localhost mydb | gzip > /backups/mydb_$(date +\%Y\%m\%d).sql.gz3. Website file backup
Archive a directory with exclusions:
0 2 * * 0 tar -czf /backups/site_$(date +\%Y\%m\%d).tar.gz -C /var/www --exclude='*/cache/*' --exclude='*/logs/*' example.comWeekly on Sunday at 2:00. Cache and logs are excluded.
4. Log cleanup
Rotate and delete old application logs:
0 0 * * * find /var/www/example.com/storage/logs -name "*.log" -mtime +30 -deleteDelete old Nginx/Apache logs (if logrotate is not used):
0 1 1 * * find /var/log/nginx -name "*.gz" -mtime +90 -delete5. Temporary file cleanup
0 4 * * * find /tmp -type f -atime +7 -delete
0 4 * * * find /var/www/*/storage/framework/cache -type f -mtime +1 -deleteThe first line removes general temporary files older than 7 days. The second clears the Laravel cache (adapt the path).
6. WordPress: updates and optimization
Use WP-CLI to update and optimize the database:
0 4 * * 0 cd /var/www/wordpress && wp core update --quiet && wp plugin update --all --quiet && wp db optimize --quietWeekly on Sunday at 4:00. Add --path=/path/to/wordpress if needed.
7. WordPress: backup with WP-CLI
0 3 * * * cd /var/www/wordpress && wp db export - | gzip > /backups/wp_$(date +\%Y\%m\%d).sql.gz8. Laravel: scheduler
Laravel uses a single cron entry for the entire schedule:
* * * * * cd /var/www/laravel && php artisan schedule:run >> /dev/null 2>&1Tasks are defined in app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('03:00');
$schedule->command('queue:prune-failed')->daily();
$schedule->command('cache:prune-stale-tags')->hourly();
}9. PHP session cleanup
Old sessions in /var/lib/php/sessions:
0 5 * * * find /var/lib/php/sessions -type f -mmin +1440 -deleteSessions older than 24 hours (1440 minutes) are deleted. Configure this to match session.gc_maxlifetime in php.ini.
10. SSL certificate check
Send a notification 14 days before expiration (using certbot or openssl):
0 9 * * * /opt/scripts/check-ssl.shExample script:
#!/bin/bash
DOMAIN="example.com"
EXPIRY=$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
EXPIRY_EPOCH=$(date -d "$EXPIRY" +%s)
NOW_EPOCH=$(date +%s)
DAYS_LEFT=$(( (EXPIRY_EPOCH - NOW_EPOCH) / 86400 ))
if [ $DAYS_LEFT -lt 14 ]; then
echo "SSL истекает через $DAYS_LEFT дней" | mail -s "SSL Alert" admin@example.com
fi11. Synchronization with remote storage
Upload backups to S3 or another object storage service:
0 5 * * * /usr/bin/aws s3 sync /backups s3://my-bucket/backups/ --deleteWith encryption and a time limit (rclone):
0 5 * * * rclone sync /backups remote:backups --max-age 7d12. Docker cleanup
Remove unused images and containers:
0 6 * * 0 docker system prune -af --volumesWeekly. Make sure important volumes will not be deleted. A gentler option without --volumes:
0 6 * * 0 docker image prune -af && docker container prune -f13. Send a website status report
Check availability and send a report:
0 9 * * 1-5 /opt/scripts/uptime-report.shThe script can use curl to check HTTP and send the result by email or messenger.
14. Clear the mail queue (mailq)
If email is sent through a local MTA:
0 2 * * * find /var/spool/mail -type f -mtime +7 -deleteFor Postfix—flush stuck messages (use with caution):
0 3 * * * postsuper -d ALL deferredUse only if you understand the consequences.
15. Generate a sitemap
For static websites or custom CMSs:
0 6 * * * /usr/bin/php /var/www/example.com/generate-sitemap.phpOr use curl to call an internal endpoint:
0 6 * * * curl -s -o /dev/null https://example.com/cron/generate-sitemap?key=SECRETThe endpoint must be protected with a secret key and inaccessible from outside.
General recommendations
| Practice | Description |
|---|---|
| Lockfile | Use flock or lock files for long-running tasks |
| Logging | Write logs to files with rotation |
| Notifications | Set up alerts on errors (exit code != 0) |
| Testing | Run scripts manually before adding them to cron |
| Documentation | Comment on jobs in crontab |
Example with a lockfile and logging:
0 3 * * * flock -n /tmp/backup.lock /opt/backup.sh >> /var/log/backup.log 2>&1 || echo "Backup already running" | mail -s "Cron alert" admin@example.comThe basic syntax and debugging of cron are described in the cron jobs guide. You can check and generate a schedule with the Cron Generator on reChecker.
Additional scenarios
Clean up old Laravel sessions
0 4 * * * find /var/www/*/storage/framework/sessions -type f -mmin +10080 -deleteDelete sessions older than 7 days (10080 minutes). Adapt the path to your project structure.
Synchronize with a remote repository
0 2 * * * cd /var/www/example.com && git pull origin main 2>&1 | logger -t git-syncUpdate the code daily. Logs are written to syslog with the tag git-sync.
Check website availability
*/5 * * * * curl -sf -o /dev/null https://example.com/health || echo "Site down" | mail -s "Uptime alert" admin@example.comEvery 5 minutes. If the site is unavailable, send an email. For production, specialized monitoring services are preferable.
Docker cleanup
0 6 * * 0 docker image prune -af && docker container prune -fWeekly removal of unused images and containers. Without -f, confirmation will be required.
Integration tips
When working with cron and web applications:
- Use queues — for heavy tasks (email campaigns, reports), it is better to enqueue them (Redis, RabbitMQ) and process them with workers. Cron only adds the tasks.
- Secure cron endpoints — if a task is called over HTTP, use a secret key in the URL and check the IP.
- Document jobs — at the beginning of crontab or in a separate file, describe the purpose of each job.
Summary
15 ready-made jobs cover the main scenarios: backups, cleanup, updates, and monitoring. Adapt paths and parameters to your infrastructure. Test scripts manually before adding them to cron.