0Pricing
DevOps Bootcamp · Lesson

Scheduling Tasks with Cron

Master the 'cron' utility to schedule scripts and commands to run automatically at specified intervals or times.

Scheduling Tasks with Cron is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Automate Tasks with Cron

Welcome to the world of automation! In this lesson, you'll learn about cron, a powerful utility in Linux that lets you schedule commands or scripts to run automatically at specific times.

Imagine setting up daily backups or hourly system checks without lifting a finger – that's cron's job!

Your Personal Crontab File

Each user on a Linux system has their own crontab (cron table) file. This is where you store all your scheduled tasks.

To edit your personal crontab, you'll use the command crontab -e. This opens the file in your default text editor, ready for you to add new jobs.

Crontab Entry: Time Fields

A crontab entry consists of six parts. The first five define when your command will run, and they are separated by spaces:

  • Minute: 0-59
  • Hour: 0-23 (0 is midnight)
  • Day of Month: 1-31
  • Month: 1-12 (or Jan-Dec)
  • Day of Week: 0-7 (0 or 7 is Sunday, 1 is Monday)

Crontab Entry: The Command

The sixth and final part of a crontab entry is the command or script you want to execute.

It's crucial to use absolute paths for commands and scripts here (e.g., /usr/bin/python instead of just python). Cron jobs run in a minimal environment, so your usual PATH might not be available.

Special Crontab Characters

To make your schedules flexible, cron uses special characters:

  • * (asterisk): Matches all possible values (e.g., every minute, every hour).
  • , (comma): Separates a list of values (e.g., 1,15 for minutes 1 and 15).
  • - (hyphen): Defines a range of values (e.g., 9-17 for hours 9 through 17).
  • / (slash): Specifies step values (e.g., */10 for every 10 minutes).

Predefined Scheduling Shortcuts

For common scheduling needs, cron offers convenient shortcuts that replace the first five time fields:

  • @reboot: Runs once after system reboot.
  • @yearly (or @annually): Runs once a year (0 0 1 1 *).
  • @monthly: Runs once a month (0 0 1 * *).
  • @weekly: Runs once a week (0 0 * * 0).
  • @daily (or @midnight): Runs once a day (0 0 * * *).
  • @hourly: Runs once an hour (0 * * * *).

Hands-on: Schedule a Simple Script

Let's create a simple script that logs a greeting, then schedule it to run every day at 10:00 AM.

  1. Create the script (e.g., ~/greet.sh):
  2. Make it executable: chmod +x ~/greet.sh
  3. Add to crontab (crontab -e):
    0 10 * * * /home/user/greet.sh
    This entry means: At minute 0, hour 10 (10 AM), every day of the month, every month, every day of the week, run /home/user/greet.sh.
#!/bin/bash
# This script logs a greeting to a temporary file.
echo "Hello from Cron! The time is $(date)" >> /tmp/cron_greeting.log

Managing Your Cron Jobs

Once you've added cron jobs, you'll need to manage them:

  • To list your current crontab entries: crontab -l
  • To remove all your crontab entries (use with extreme caution, as there's no undo!): crontab -r

Always review your crontab with crontab -l after making changes to ensure your entries are correct.

Cron's Environment Variables

Remember that cron jobs run in a minimal environment. This means they often don't have the same environment variables (like PATH) as your interactive shell.

If your script relies on specific commands, use their absolute paths or define necessary environment variables directly within your crontab file at the top, like PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.

Crontab Syntax Challenge

You need to schedule a script named /home/user/daily_report.sh to run at 9:15 AM every weekday (Monday to Friday). Which crontab entry is correct?

Recap: Mastered Cron Scheduling

Great job! You've now mastered the fundamentals of cron for scheduling tasks.

  • You learned about the crontab file and how to edit it with crontab -e.
  • You understand the six fields of a cron entry, including time fields and the command.
  • You explored special characters and predefined shortcuts for flexible scheduling.
  • You know how to manage your jobs with crontab -l and crontab -r.
  • You also learned about the importance of absolute paths and environment variables in cron's context.

Now go forth and automate!

Frequently asked questions

Is the “Scheduling Tasks with Cron” lesson free?

Yes — the full text of “Scheduling Tasks with Cron” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Scheduling Tasks with Cron”?

Master the 'cron' utility to schedule scripts and commands to run automatically at specified intervals or times. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scheduling Tasks with Cron” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Scheduling Tasks with Cron
  2. Automating System Administration
  3. Advanced Scripting Project
  4. Automated Log Rotation & Cleanup
← Back to DevOps Bootcamp