Schedule Tasks Using at and Cron

Red Hat Enterprise LinuxBeginner
Practice Now

Introduction

As a system administrator, automating tasks is a core responsibility. The at and cron utilities are fundamental tools for scheduling tasks on Linux. In this challenge, you will practice using at to schedule a one-time job and cron to schedule a recurring job.

Schedule a One-Time Task with at

The at command is used to schedule commands to be executed once at a particular time in the future.

Tasks

  • Use the at command to schedule a task to run in one minute.
  • Verify that the task creates a file with the specified content.

Requirements

  • Schedule a task to run one minute from now.
  • The task must execute the command echo "This is a scheduled task" > /home/labex/project/scheduled_task.txt.
  • After the task executes, the file /home/labex/project/scheduled_task.txt must exist and contain the text This is a scheduled task.

Example

After the task has executed, the content of /home/labex/project/scheduled_task.txt should be:

This is a scheduled task

Hints

  • Use the time specification now + 1 minute to schedule the task.
  • After entering the at command, you will be at a new prompt. Type your command and press Enter.
  • To save the job and exit the at prompt, press Ctrl+D.

Schedule a Recurring Task with cron

The cron daemon is used to execute scheduled commands at regular intervals. You can manage your scheduled tasks using the crontab command.

Tasks

  • Create a cron job that runs a command every minute.
  • Verify that a log file is created and updated by the cron job.

Requirements

  • Use the crontab -e command to edit your user's crontab.
  • Add a cron job that executes the command echo "This is a recurring task" >> /home/labex/project/recurring_task.log.
  • The job must be configured to run every minute of every hour, every day.
  • The file /home/labex/project/recurring_task.log should be created and appended with new text every minute.

Example

After the cron job has run for a few minutes, the contents of /home/labex/project/recurring_task.log should look similar to this:

This is a recurring task
This is a recurring task
This is a recurring task

Hints

  • The crontab -e command will open your crontab file in a text editor (usually vi).
  • The format for a cron job that runs every minute is * * * * * command_to_run.
  • If the editor is vi, press i to enter insert mode, type your line, press Esc to exit insert mode, and then type :wq and press Enter to save and quit.

Summary

In this challenge, you have learned how to automate tasks on a Linux system. You practiced scheduling a one-time job for future execution using the at command and setting up a recurring task with cron by editing a crontab. These are essential skills for any system administrator to manage routine maintenance and other automated processes.

✨ Check Solution and Practice✨ Check Solution and Practice