Cron, automation simplified.

Posted on July 01, 2025 in Linux

Cron: My Automation Sidekick

Linux’s cron is the trusty sidekick for scheduling tasks, like a robot butler that runs your scripts exactly when you want. It’s perfect for automating backups, monitoring, or running your Python and Docker apps (like those in your craigderington Docker Hub repo). While cron isn’t as obscure as some Linux tools, its flexibility and logging tricks are often underused. This post dives into cron’s scheduling magic, with examples of different schedules and tips for piping output to logs, keeping your Notes site practical and awesome.

Why Cron?

Cron schedules jobs to run at specific times or intervals, from hourly backups to daily cleanup scripts. It’s built into every Linux system, reliable, and simple, making it ideal for automating tasks like checking your Docker containers or syncing data. Setting Up Cron Cron jobs are defined in a crontab file. Edit yours with: crontab -e

This opens your user’s crontab in your default editor (e.g., nano or vim). Each line is a job with a schedule and command. Cron Schedule Basics A cron schedule has five fields: minute, hour, day of month, month, day of week. The format is: * * * * * /path/to/script.sh

  • means “every” (e.g., every minute if in the minute field). Numbers, ranges, or lists specify exact times. Special characters like / (step) or , (list) add flexibility.

Example Schedules 1. Run Every Minute For testing or frequent tasks (e.g., checking a Docker container): * * * * * /bin/bash /scripts/check_docker.sh >> /logs/docker.log 2>&1

Runs every minute.

/logs/docker.log 2>&1 appends stdout and stderr to docker.log.

  1. Daily at Midnight Backup your craigderington/docker-python app data: 0 0 * * * /bin/bash /scripts/backup.sh >> /logs/backup.log 2>&1

Runs at 00:00 (midnight) daily. Logs output to backup.log.

  1. Every Weekday at 8 AM Run a Python script to process logs: 0 8 * * 1-5 /usr/bin/python3 /scripts/process_logs.py >> /logs/process.log 2>&1

Runs at 08:00 Monday–Friday (1-5). Redirects output to process.log.

  1. Every 15 Minutes Monitor system resources: */15 * * * * /bin/bash /scripts/monitor.sh >> /logs/monitor.log 2>&1

Runs every 15 minutes (e.g., 00:15, 00:30). */15 is a step value.

  1. First Day of Month Clear old Docker images: 0 0 1 * * /usr/bin/docker system prune -f >> /logs/docker_prune.log 2>&1

Runs at 00:00 on the 1st of each month.

  1. Specific Times (e.g., 9 AM and 5 PM) Run a Java app health check: 0 9,17 * * * /usr/bin/java -jar /apps/healthcheck.jar >> /logs/healthcheck.log 2>&1

Runs at 09:00 and 17:00 daily. 9,17 lists multiple hours.

Logging Output Piping output to logs is crucial for debugging and tracking. The >> appends output, while 2>&1 captures errors too. Example script (check_docker.sh):

!/bin/bash

echo "Checking containers at $(date)" docker ps -q | grep craigderington/docker-nginx || echo "Nginx container down!"

Add to crontab: * * * * * /bin/bash /scripts/check_docker.sh >> /logs/docker.log 2>&1

Check the log: cat /logs/docker.log

Output: Checking containers at Thu Jul 17 12:12:01 EDT 2025

Advanced Features Environment Variables Cron has a minimal environment. Set variables in the crontab: PATH=/usr/local/bin:/usr/bin:/bin DOCKER_HOST=unix:///var/run/docker.sock 0 * * * * /scripts/run_docker.sh >> /logs/docker.log 2>&1

This ensures your script can find docker or other tools. Special Schedules Use shortcuts for common schedules:

@daily: Same as 0 0 * * . @hourly: Same as 0 * * * . @reboot: Runs once at system startup.Example: Clean temp files on reboot:

@reboot /bin/bash /scripts/clean_tmp.sh >> /logs/clean.log 2>&1

Error Notifications Send errors to email or a file. Add MAILTO to your crontab: MAILTO=admin@example.com 0 0 * * * /scripts/backup.sh >> /logs/backup.log 2>&1

Or log only errors: 0 0 * * * /scripts/backup.sh >/dev/null 2>>/logs/backup_errors.log

/dev/null: Discards stdout. 2>>: Appends stderr to backup_errors.log.

Example: Full Backup Workflow Here’s a crontab for a backup system using your craigderington/docker-python image:

Backup app data daily at 2 AM

0 2 * * * /usr/bin/docker run --rm craigderington/docker-python /app/backup.py >> /logs/backup.log 2>&1

Clean old backups weekly on Sunday

0 0 * * 0 /bin/bash /scripts/clean_old_back elegir ups.sh >> /logs/cleanup.log 2>&1

Notify on errors

MAILTO=admin@example.com

backup.py might look like: import shutil import datetime shutil.make_archive(f"backup-{datetime.date.today()}", "zip", "/app/data") print(f"Backup created at {datetime.datetime.now()}")

Why Cron Shines Cron is the unsung hero of Linux automation. It’s dead simple, universally available, and perfect for scheduling tasks like monitoring your Docker containers, running Python scripts, or maintaining PostgreSQL databases. Logging ensures you can debug issues, while flexible schedules handle everything from minute-by-minute checks to monthly cleanups. Pro Tips

Test first: Run crontab -l to list and verify your jobs. Use absolute paths: Avoid errors with /usr/bin/python3 instead of python3. Lock files: Prevent overlapping jobs:# In script.sh [ -f /tmp/script.lock ] && exit 1 touch /tmp/script.lock

Your code

rm /tmp/script.lock

Check logs: Use grep CRON /var/log/syslog (or /var/log/cron) to debug cron issues.

Cron’s simplicity makes it a must-have for your Notes site, especially for automating tasks with your craigderington Docker images. Schedule, log, and relax—cron’s got your back!