bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Bash/Scripting
Bash•Scripting

Bash crontab Command - Schedule Tasks

Understanding Cron and Crontab

The cron system is a time-based job scheduler in Unix-like operating systems.

It automates the execution of tasks (known as cron jobs) at specified intervals.

While cron is the background service that runs these tasks, crontab is the command used to manage them.

There is no direct "cron" command; instead, you use crontab to set up and control cron jobs.

Using Crontab

The crontab command allows you to define scheduled tasks.

These tasks are specified in a crontab file, which is a simple text file containing a list of commands meant to be run at specified times.

Crontab Syntax

The basic syntax of the crontab command is:

crontab [options]

Options

  • -e : Edit the crontab file for the current user.
  • -l : List the crontab entries for the current user.
  • -r : Remove the crontab file for the current user.

Setting Up Cron Jobs

Cron jobs are defined using a specific syntax in the crontab file. Each line in the file represents a task and follows this format:

* * * * * command_to_execute
  • Minute : 0-59
  • Hour : 0-23
  • Day of Month : 1-31
  • Month : 1-12
  • Day of Week : 0-7 (0 and 7 are Sunday)

Each asterisk can be replaced with a specific value or range to schedule the command at specific times.

Example: Schedule a Task

To run a script every day at midnight, you would use:

0 0 * * * /path/to/script.sh

This entry will execute /path/to/script.sh every day at 00:00 (midnight).

Common Uses

Cron jobs are commonly used to

  • Automate system maintenance tasks, like backups and updates.
  • Schedule scripts to run at specific times or intervals.
  • Perform regular monitoring and reporting tasks.

Previous

Bash Arrays

This chapter

Overview
10

Lessons

20m

Read time

1. Basic Bash Syntax2. Bash Script3. Bash Variables4. Bash Data Types5. Bash Operators6. Bash If...Else7. Bash Loops8. Bash Functions9. Bash Arrays10. Bash crontab Command - Schedule Tasks

On this page

Understanding Cron and CrontabUsing CrontabCrontab SyntaxOptionsSetting Up Cron JobsExample: Schedule a TaskCommon Uses