┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 7) (0 and 7 are Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * * command-to-execute
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 7) (0 and 7 are Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * * command-to-execute
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 7) (0 and 7 are Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * * command-to-execute
0 2 * * * /home/user/backup.sh
/home/user/backup.sh
*/15 * * * * /usr/bin/python3 /home/user/check_status.py
0 0 1 * * /home/user/monthly_report.sh
30 3 * * 1 /home/user/weekly_backup.sh
@yearly 0 0 1 1 * (once per year on January 1st)
@monthly 0 0 1 * * (once per month on the 1st)
@weekly 0 0 * * 0 (once per week on Sunday at midnight)
@daily 0 0 * * * (once per day at midnight)
@hourly 0 * * * * (once per hour)
@reboot - (when the system boots up)
@yearly 0 0 1 1 * (once per year on January 1st)
@monthly 0 0 1 * * (once per month on the 1st)
@weekly 0 0 * * 0 (once per week on Sunday at midnight)
@daily 0 0 * * * (once per day at midnight)
@hourly 0 * * * * (once per hour)
@reboot - (when the system boots up)
@yearly 0 0 1 1 * (once per year on January 1st)
@monthly 0 0 1 * * (once per month on the 1st)
@weekly 0 0 * * 0 (once per week on Sunday at midnight)
@daily 0 0 * * * (once per day at midnight)
@hourly 0 * * * * (once per hour)
@reboot - (when the system boots up)
crontab -e
nano /home/user/backup_logs.sh
nano /home/user/backup_logs.sh
nano /home/user/backup_logs.sh
#!/bin/bash BACKUP_DIR="/backups"
SOURCE_DIR="/var/log"
DATE=$(date +%Y%m%d_%H%M%S) mkdir -p $BACKUP_DIR tar -czf $BACKUP_DIR/logs_backup_$DATE.tar.gz $SOURCE_DIR find $BACKUP_DIR -name "logs_backup_*.tar.gz" -mtime +7 -delete echo "Backup completed at $(date)" >> /var/log/backup.log
#!/bin/bash BACKUP_DIR="/backups"
SOURCE_DIR="/var/log"
DATE=$(date +%Y%m%d_%H%M%S) mkdir -p $BACKUP_DIR tar -czf $BACKUP_DIR/logs_backup_$DATE.tar.gz $SOURCE_DIR find $BACKUP_DIR -name "logs_backup_*.tar.gz" -mtime +7 -delete echo "Backup completed at $(date)" >> /var/log/backup.log
#!/bin/bash BACKUP_DIR="/backups"
SOURCE_DIR="/var/log"
DATE=$(date +%Y%m%d_%H%M%S) mkdir -p $BACKUP_DIR tar -czf $BACKUP_DIR/logs_backup_$DATE.tar.gz $SOURCE_DIR find $BACKUP_DIR -name "logs_backup_*.tar.gz" -mtime +7 -delete echo "Backup completed at $(date)" >> /var/log/backup.log
chmod +x /home/user/backup_logs.sh
chmod +x /home/user/backup_logs.sh
chmod +x /home/user/backup_logs.sh
crontab -e
0 3 * * * /home/user/backup_logs.sh
0 3 * * * /home/user/backup_logs.sh
0 3 * * * /home/user/backup_logs.sh
crontab -l
0 */6 * * * /usr/bin/mysqldump -u root -pYourPassword database_name > /backups/db_$(date +\%Y\%m\%d_\%H\%M\%S).sql
0 */6 * * * /usr/bin/mysqldump -u root -pYourPassword database_name > /backups/db_$(date +\%Y\%m\%d_\%H\%M\%S).sql
0 */6 * * * /usr/bin/mysqldump -u root -pYourPassword database_name > /backups/db_$(date +\%Y\%m\%d_\%H\%M\%S).sql
0 2 * * * find /var/log/nginx -name "*.log" -mtime +30 -delete
0 2 * * * find /var/log/nginx -name "*.log" -mtime +30 -delete
0 2 * * * find /var/log/nginx -name "*.log" -mtime +30 -delete
30 4 * * * rsync -avz --delete /home/user/data/ [email protected]:/backup/data/
30 4 * * * rsync -avz --delete /home/user/data/ [email protected]:/backup/data/
30 4 * * * rsync -avz --delete /home/user/data/ [email protected]:/backup/data/
*/5 * * * * /home/user/check_uptime.sh
*/5 * * * * /home/user/check_uptime.sh
*/5 * * * * /home/user/check_uptime.sh
check_uptime.sh
#!/bin/bash WEBSITE="https://example.com"
LOGFILE="/var/log/uptime_check.log" if curl -s --max-time 5 "$WEBSITE" > /dev/null; then echo "$(date) - $WEBSITE is UP" >> $LOGFILE
else echo "$(date) - $WEBSITE is DOWN - sending alert" >> $LOGFILE echo "Website down!" | mail -s "Alert: $WEBSITE Down" [email protected]
fi
#!/bin/bash WEBSITE="https://example.com"
LOGFILE="/var/log/uptime_check.log" if curl -s --max-time 5 "$WEBSITE" > /dev/null; then echo "$(date) - $WEBSITE is UP" >> $LOGFILE
else echo "$(date) - $WEBSITE is DOWN - sending alert" >> $LOGFILE echo "Website down!" | mail -s "Alert: $WEBSITE Down" [email protected]
fi
#!/bin/bash WEBSITE="https://example.com"
LOGFILE="/var/log/uptime_check.log" if curl -s --max-time 5 "$WEBSITE" > /dev/null; then echo "$(date) - $WEBSITE is UP" >> $LOGFILE
else echo "$(date) - $WEBSITE is DOWN - sending alert" >> $LOGFILE echo "Website down!" | mail -s "Alert: $WEBSITE Down" [email protected]
fi
0 8 * * * /usr/bin/python3 /home/user/generate_report.py
0 8 * * * /usr/bin/python3 /home/user/generate_report.py
0 8 * * * /usr/bin/python3 /home/user/generate_report.py
#!/usr/bin/env python3 import subprocess
from datetime import datetime report_date = datetime.now().strftime("%Y-%m-%d")
output_file = f"/reports/report_{report_date}.txt" with open(output_file, "w") as f: f.write(f"Daily Report for {report_date}\n") f.write("=" * 40 + "\n") disk_usage = subprocess.check_output(["df", "-h"], text=True) f.write("Disk Usage:\n") f.write(disk_usage) f.write("\n") memory = subprocess.check_output(["free", "-h"], text=True) f.write("Memory Status:\n") f.write(memory) print(f"Report generated: {output_file}")
#!/usr/bin/env python3 import subprocess
from datetime import datetime report_date = datetime.now().strftime("%Y-%m-%d")
output_file = f"/reports/report_{report_date}.txt" with open(output_file, "w") as f: f.write(f"Daily Report for {report_date}\n") f.write("=" * 40 + "\n") disk_usage = subprocess.check_output(["df", "-h"], text=True) f.write("Disk Usage:\n") f.write(disk_usage) f.write("\n") memory = subprocess.check_output(["free", "-h"], text=True) f.write("Memory Status:\n") f.write(memory) print(f"Report generated: {output_file}")
#!/usr/bin/env python3 import subprocess
from datetime import datetime report_date = datetime.now().strftime("%Y-%m-%d")
output_file = f"/reports/report_{report_date}.txt" with open(output_file, "w") as f: f.write(f"Daily Report for {report_date}\n") f.write("=" * 40 + "\n") disk_usage = subprocess.check_output(["df", "-h"], text=True) f.write("Disk Usage:\n") f.write(disk_usage) f.write("\n") memory = subprocess.check_output(["free", "-h"], text=True) f.write("Memory Status:\n") f.write(memory) print(f"Report generated: {output_file}")
*/10 * * * * /home/user/check_service.sh
*/10 * * * * /home/user/check_service.sh
*/10 * * * * /home/user/check_service.sh
#!/bin/bash SERVICE_NAME="nginx" if ! systemctl is-active --quiet $SERVICE_NAME; then echo "$(date) - $SERVICE_NAME was down, restarting..." >> /var/log/service_restarts.log systemctl restart $SERVICE_NAME
fi
#!/bin/bash SERVICE_NAME="nginx" if ! systemctl is-active --quiet $SERVICE_NAME; then echo "$(date) - $SERVICE_NAME was down, restarting..." >> /var/log/service_restarts.log systemctl restart $SERVICE_NAME
fi
#!/bin/bash SERVICE_NAME="nginx" if ! systemctl is-active --quiet $SERVICE_NAME; then echo "$(date) - $SERVICE_NAME was down, restarting..." >> /var/log/service_restarts.log systemctl restart $SERVICE_NAME
fi
sudo service cron status
sudo service cron status
sudo service cron status
sudo systemctl status cron
sudo systemctl status cron
sudo systemctl status cron
sudo systemctl start cron
sudo systemctl enable cron
sudo systemctl start cron
sudo systemctl enable cron
sudo systemctl start cron
sudo systemctl enable cron
sudo grep CRON /var/log/syslog
sudo grep CRON /var/log/syslog
sudo grep CRON /var/log/syslog
sudo journalctl -u cron --no-pager
sudo journalctl -u cron --no-pager
sudo journalctl -u cron --no-pager
Nov 15 03:00:01 myserver CRON[12345]: (user) CMD (/home/user/backup_logs.sh)
Nov 15 03:00:01 myserver CRON[12345]: (user) CMD (/home/user/backup_logs.sh)
Nov 15 03:00:01 myserver CRON[12345]: (user) CMD (/home/user/backup_logs.sh)
0 3 * * * /home/user/backup_
0 3 * * * /home/user/backup_
0 3 * * * /home/user/backup_ - n8n Cloud or self-hosted n8n (optional, for automation integration)
- Hetzner VPS, Contabo VPS, or DigitalOcean for running cron jobs
- Namecheap if you need a domain for your automation
- Linux server access (Ubuntu 20.04 LTS or later recommended)
- Basic command-line familiarity - Understanding Cron: The Basics
- Crontab Syntax Breakdown
- Creating and Managing Your First Cron Job
- Real-World Examples and Use Cases
- Debugging and Monitoring Cron Jobs
- Getting Started