8. Cron Jobs

While process utilization is important, it's also a great time to introduce a powerful tool for Linux automation: the cron daemon. This background service allows you to schedule tasks to run automatically at specific times or intervals. These scheduled tasks are commonly known as cron jobs. This is incredibly useful for automating routine actions, such as running a backup script every night or cleaning up temporary files once a week.

What are Cron Jobs

Imagine you have a script at /home/pete/scripts/change_wallpaper that you run every morning to set a new desktop background. Instead of running it manually each day, you can create a cron job to execute it for you. By defining a schedule, you can tell the cron service exactly when to run your script, making it a true "set it and forget it" solution.

Understanding Crontab Syntax

To create a cron job, you need to specify the schedule and the command to run. The schedule is defined by five fields, followed by the command.

30 08 * * * /home/pete/scripts/change_wallpaper

The five time-and-date fields are, from left to right:

  • Minute: 0-59
  • Hour: 0-23 (in 24-hour format)
  • Day of the month: 1-31
  • Month: 1-12
  • Day of the week: 0-7 (where both 0 and 7 represent Sunday)

An asterisk (*) in a field acts as a wildcard, meaning "every". In the example above, the schedule 30 08 * * * tells cron to run the command at 8:30 AM, every day of the month, every month, and every day of the week.

Managing Cron Jobs with Crontab

You manage your personal cron jobs using the crontab command, which allows you to edit your user-specific crontab file. This file holds all the cron jobs you have scheduled.

To add or edit your cron jobs, use the -e (edit) flag. This will open your crontab file in your default text editor.

crontab -e

Once you add your job definition and save the file, cron will automatically read the new schedule. You can also list your active cron jobs with crontab -l or remove all of them with crontab -r. Using cron jobs is a fundamental skill for anyone interested in Linux automation.

Sign in to save your learning progress

Sign in

Exercises

Practice makes perfect! This hands-on lab will help reinforce your understanding of how to schedule tasks in Linux.

  1. Schedule Tasks with at and cron in Linux - Practice creating, managing, and removing jobs with at, atq, atrm, and crontab, gaining hands-on experience in automating system administration tasks.

This lab will help you apply the concepts from this lesson in a real-world scenario and build your confidence with Linux automation.

Quiz

What is the command to edit your personal cron jobs? (Please answer using the exact lowercase command and its option.)