Set Up Scheduled Tasks

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to set up scheduled tasks on Linux. You will learn how to use the watch command and the crontab command to schedule a task to run repeatedly.

Achievements

  • watch - Run a command repeatedly
  • crontab - Schedule a task to run repeatedly

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux/SystemInformationandMonitoringGroup -.-> linux/watch("`Command Repeating`") linux/SystemInformationandMonitoringGroup -.-> linux/crontab("`Job Scheduling`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/time("`Command Timing`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/watch -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/crontab -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/echo -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/vim -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/date -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/time -.-> lab-47{{"`Set Up Scheduled Tasks`"}} shell/comments -.-> lab-47{{"`Set Up Scheduled Tasks`"}} shell/quoting -.-> lab-47{{"`Set Up Scheduled Tasks`"}} shell/globbing_expansion -.-> lab-47{{"`Set Up Scheduled Tasks`"}} linux/wildcard -.-> lab-47{{"`Set Up Scheduled Tasks`"}} end

Run Command Repeatedly

watch is a command that runs another command repeatedly at a specified interval. It is useful for scheduling tasks that need to be run repeatedly.

To run a command repeatedly, use the watch command with the -n option. The -n option specifies the interval in seconds between each run of the command.

Tips: The default interval is 2 seconds.

## display the current time every 5 seconds
watch -n 5 date
lab-set-up-scheduled-tasks-1-1.png

Set Up Scheduled Jobs

When you need to run a command repeatedly, you can use the watch command. You can also use the crontab command, it's more powerful and useful for scheduling tasks that need to be run repeatedly.

The crontab command uses a special format to specify the time and date to run a command. The format is as follows:

* * * * * command

The * character is a wildcard that matches any value for that field. The fields are as follows:

  • Minute (0-59)
  • Hour (0-23)
  • Day of month (1-31)
  • Month (1-12)
  • Day of week (0-6, 0 is Sunday)

To run a command repeatedly, use the crontab command with the -e option. The -e option opens the crontab file in the default editor.

Tips: The default editor is vi.

## Open the crontab file in the vim editor
crontab -e
lab-set-up-scheduled-tasks-2-1.png

Add the following line to the crontab file:

* * * * * date

This line means that the date command will be run every minute.

Inpress Esc to exit the insert mode, then type :wq to save and exit the file.

You can also set another date to run command, such as:

## Run the echo command in 08:07:30 everyday
30 7 8 * * * echo "Hello World"

Check Crontab List

To check the crontab list, use the crontab command with the -l option. The -l option lists the crontab file.

crontab -l

Summary

Congratulations! You have completed the lab.

In this lab, you learned how to set up scheduled tasks on Linux. You learned how to use the watch command and the crontab command to schedule a task to run repeatedly.

Keep exploring the Linux system!

Other Linux Tutorials you may like