Cron Jobs: Scheduling Automatic Tasks on Your Server

Backup at 3 AM, daily reports, temp file cleanup — Cron automates everything. We explain with practical examples.
What Are Cron Jobs?
Cron is the task scheduler in Linux systems. It allows you to run commands or scripts automatically at specified times — every minute, every hour, every day, or any schedule you define. Without Cron, you would need to log in manually to execute recurring tasks.
Common uses:
- Daily database backups
- Deleting temporary files and old logs
- Sending periodic reports
- Renewing SSL certificates
- Monitoring service health and restarting them
- Running Laravel scheduled commands
Cron Syntax
Each line in Crontab follows this format:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * * /path/to/command
Schedule Examples
# Every minute
* * * * * /path/to/script.sh
# Every 5 minutes
*/5 * * * * /path/to/script.sh
# Every hour at minute 0
0 * * * * /path/to/script.sh
# Daily at 3 AM
0 3 * * * /path/to/script.sh
# Every Monday at 9 AM
0 9 * * 1 /path/to/script.sh
# First day of every month
0 0 1 * * /path/to/script.sh
# Every 15 minutes during business hours (8AM - 6PM)
*/15 8-18 * * * /path/to/script.sh
# Saturday and Sunday at 2 AM
0 2 * * 0,6 /path/to/script.sh
Managing Crontab
# View current jobs
crontab -l
# Edit jobs
crontab -e
# View jobs for a specific user
sudo crontab -u www-data -l
# Delete all jobs (use with caution)
crontab -r
Common Practical Tasks
Database Backup
# File: /opt/scripts/backup-db.sh
#!/bin/bash
BACKUP_DIR="/opt/backups/mysql"
DATE=$(date +%Y-%m-%d_%H%M)
DB_NAME="production_db"
DB_USER="backup_user"
DB_PASS="secure_password"
# Create backup directory
mkdir -p $BACKUP_DIR
# Create the backup
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > "$BACKUP_DIR/${DB_NAME}_${DATE}.sql.gz"
# Delete backups older than 30 days
find $BACKUP_DIR -name "*.sql.gz" -mtime +30 -delete
# Log the operation
echo "$(date): Backup completed - ${DB_NAME}_${DATE}.sql.gz" >> /var/log/backup.log
# Run daily at 2 AM
0 2 * * * /opt/scripts/backup-db.sh
Log and Temporary File Cleanup
# File: /opt/scripts/cleanup.sh
#!/bin/bash
# Delete logs older than 14 days
find /var/log -name "*.log" -mtime +14 -delete
find /var/log -name "*.gz" -mtime +30 -delete
# Delete tmp files
find /tmp -type f -mtime +7 -delete
# Clean Laravel logs
find /var/www/app/storage/logs -name "*.log" -mtime +7 -delete
# Log disk usage after cleanup
echo "$(date): Disk after cleanup: $(df -h / | tail -1 | awk '{print $5}')" >> /var/log/cleanup.log
# Run daily at 4 AM
0 4 * * * /opt/scripts/cleanup.sh
Service Monitoring
# File: /opt/scripts/monitor.sh
#!/bin/bash
check_service {
if ! systemctl is-active --quiet $1; then
systemctl restart $1
echo "$(date): $1 was down - restarted" >> /var/log/monitor.log
fi
}
check_service nginx
check_service mysql
check_service php8.3-fpm
check_service redis-server
# Check every 5 minutes
*/5 * * * * /opt/scripts/monitor.sh
SSL Certificate Renewal
# Auto-renew twice a week
0 3 * * 1,4 certbot renew --quiet --post-hook "systemctl reload nginx"
Laravel Scheduled Tasks
Laravel provides an advanced scheduling system. You need only one Cron entry:
# Run Laravel scheduler every minute
* * * * * cd /var/www/app && php artisan schedule:run >> /dev/null 2>&1
Then define tasks in app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
// Send subscription expiration alerts daily
$schedule->command('subscriptions:notify-expiring')
->dailyAt('09:00');
// Clean expired sessions
$schedule->command('session:gc')
->hourly;
// Daily report
$schedule->command('reports:daily')
->dailyAt('23:00')
->emailOutputTo('admin@example.com');
}
Debugging
Verify Cron is Running
# Is the cron service active?
sudo systemctl status cron
# Cron logs
sudo grep CRON /var/log/syslog | tail -20
Common Errors and Solutions
| Problem | Cause | Solution |
|---|---|---|
| Job does not run | Incomplete path | Always use full absolute paths |
| Permission error | Script not executable | chmod +x script.sh |
| Missing environment variables | Cron has limited env | Define variables inside the script |
| Command works manually only | Different PATH | Use /usr/bin/php instead of php |
Test Before Scheduling
# Run the script manually first
bash /opt/scripts/backup-db.sh
# Test with cron's limited environment
env -i /bin/bash /opt/scripts/backup-db.sh
Integration with CLOUD
CLOUD (cloud.net.sa) servers come with full root access — you can modify Crontab freely and configure any number of scheduled tasks. With fast SSD storage, backup and cleanup operations complete in seconds rather than minutes.
Get Started
Want to try it yourself? Choose the right hosting plan or build your custom server with the specs you need.
