cron and crontab

Charlier "Bird" Parker is a genius-level saxophonist who has memorized every Linux command in about 2 hours. After a 10 minute interview at Red Hat, they hire him to be their Director of Training. They also assign two mentees to Bird: Miles Davis, a young intern who also plays trumpet by night, and Red Rodney, another young intern who coincidentally also plays trumpet by night.

Bird bellows in his resonant baritone voice and his pseudo-British-accent: "I'm going to make you make experts on cron and crontab. You'd better buckle up because I'm about to teach you everything I know, the same way I taught you how to play Cherokee in all 12 keys".

Bird's RHEL server is Bird_Lives_Server_01. His username is saxophone_goat@redhat.com. He's a genius, so he never runs commands as root. What is Bird going to teach Miles and Red?

Bird is going to teach Miles Davis and Red Rodney about the cron and crontab commands, which are used for scheduling tasks on a Linux system. Here's a breakdown of what Bird will cover in his training session:

  1. Introduction to Cron:

    • Bird will explain that cron is a built-in Linux utility that allows users to schedule recurring tasks or commands to run at specific intervals.
    • He will emphasize the importance of automation and how cron can help streamline repetitive tasks.
  2. Understanding the Crontab File:

    • Bird will explain that each user has their own crontab file, which contains the scheduled tasks specific to that user.
    • He will mention that the crontab file is edited using the crontab command.
  3. Accessing and Editing the Crontab:

    • Bird will demonstrate how to access and edit the crontab file using the crontab -e command.
    • He will explain that this command opens the crontab file in the default text editor, allowing users to add, modify, or remove scheduled tasks.
  4. Crontab Syntax:

    • Bird will explain the syntax used in crontab entries, including the schedule expression and the command to be executed.
    • He will teach them about the five fields that make up the schedule expression: minute, hour, day of the month, month, and day of the week.
    • Bird will provide examples of different schedule expressions, such as specific times, recurring intervals, and combinations of days.
  5. Common Crontab Examples:

    • Bird will present common examples to showcase the versatility of crontab scheduling, such as running backups, system maintenance tasks, generating reports, and sending automated emails.
    • He will discuss best practices, such as using absolute paths for commands and scripts.
  6. Troubleshooting and Monitoring:

    • Bird will highlight the importance of monitoring and troubleshooting crontab tasks.
    • He will explain how to check the system logs (/var/log/cron) to identify any issues with scheduled tasks and ensure they are running as intended.
  7. Advanced Features and Additional Resources:

    • Bird will mention some advanced features of cron, such as environment variables, redirecting output, and job chaining.
    • He will provide additional resources like man pages (man cron, man crontab) and online documentation to encourage self-learning and exploration.

Throughout the training, Bird will share his expertise, insights, and experiences with cron and crontab, empowering Miles and Red to become proficient in scheduling tasks and leveraging automation on their journey as RHEL sysadmins.

Now, let us please have Bird login into his server and explain the crontab file and several commands with examples.

Certainly! Let's have Bird login to his server and explain the crontab file along with several commands and examples.

Bird logs into his RHEL server, Bird_Lives_Server_01, using his username saxophone_goat@redhat.com. He opens the terminal and starts the training session with Miles and Red. Here's the dialogue:

Bird: Alright, Miles and Red, let's dive into the crontab file and explore some commands and examples. To access the crontab file, type crontab -e and hit Enter.

Miles and Red follow Bird's instructions and open their crontab files.

Bird: Great! In front of you, you'll see the crontab file. It consists of lines containing scheduled tasks or commands. Each line has a specific format. Let's break it down:

# Minute Hour Day_of_Month Month Day_of_Week Command

Bird points to each field as he explains.

Bird: The first five fields represent the schedule expression, and the last field is the command to be executed. For example, let's say you want to schedule a task to run every day at 8 AM. You would write:

0 8 * * * command

Bird gestures for Miles to try an example.

Miles: Got it! So if I want to run a script called backup.sh every day at 8 AM, I'd write:

0 8 * * * /path/to/backup.sh

Bird: Exactly, Miles! You're catching on quickly. Now, let's explore some useful commands and examples.

Bird continues the training session, explaining the following commands with examples:

  1. crontab -l: Lists the current crontab entries for the user.

    • Bird demonstrates by typing crontab -l and shows the existing scheduled tasks.
  2. crontab -e: Opens the crontab file for editing.

    • Bird explains that this command opens the crontab file in the default editor, allowing users to add, modify, or remove scheduled tasks.
  3. crontab -r: Removes the crontab for the user.

    • Bird mentions the importance of caution with this command, as it removes all scheduled tasks.
  4. @reboot: Special keyword to run a command once at system startup.

    • Bird shows an example using @reboot /path/to/startup_script.sh.

Bird provides additional examples and variations of schedule expressions, such as specific times, recurring intervals, and combinations of days, depending on the needs of Miles and Red.

Throughout the session, Bird encourages Miles and Red to ask questions, provides explanations for any issues they encounter, and reinforces best practices, such as using absolute paths and proper command syntax.

By the end of the training, Miles and Red gain a solid understanding of the crontab file, various commands, and how to schedule tasks effectively using cron. Bird ensures they are confident in utilizing these skills as they embark on their sysadmin journey at Red Hat.

You should also read:

Understand Jobs and CronJobs

Jonah is a RHEL Sysadmin at Google. He is studying for his CKAD exam, specifically this objective: Understand Jobs and CronJobs. Please generate…

crontab and cron

Bird is a RHEL Sysadmin at Google, a really good one. He is teaching two new hires about cron and crontab. He wants…

/var/log/cron

Sonny Stitt is a phenomenal alto saxophonist by night, and a RHEL Systems Manager at Red Hat (Chicago campus) by day. His teams…