How to Schedule Tasks with Cron Jobs: A Practical Tutorial

Cron is a time-based job scheduler on Linux and macOS. It runs scripts or commands automatically at fixed intervals, making it ideal for backups, reports, and maintenance. This tutorial shows you how to create, test, and manage cron jobs.

Cron Syntax and Common Schedules

Open your user crontab with crontab -e. Each line uses five time fields followed by the command: minute, hour, day of month, month, day of week.

Article illustration

  • 0 2 * * * /home/user/backup.sh — daily at 2:00 AM.
  • */15 * * * * /home/user/check.sh — every 15 minutes.
  • 0 9 * * 1 /home/user/report.sh — every Monday at 9:00 AM.

Step-by-Step Setup

Edit the crontab, add your job on a new line, save, and exit. Verify with crontab -l. Use absolute paths for scripts and commands.

Testing and Troubleshooting

Cron emails output if mail is configured. To log manually, redirect output: 0 2 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1. Check system logs with grep CRON /var/log/syslog.

Best Practices

  • Set PATH at the top if needed.
  • Use flock to prevent overlapping jobs.
  • Test commands in the terminal first.

Cron remains a simple, reliable way to automate repetitive tasks. Start with one job, confirm it works, then expand.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *