SKDBLOG

Scripting

Mastering Task Automation in Linux: A Complete Guide to Crontab Scheduling

Hello, everyone. In this article, I will discuss a handy feature of Linux: “Crontab.” It is used to schedule tasks and is one of the most useful features for server administrators.

Shashikant Dwivedi
3 min read
Mastering Task Automation in Linux: A Complete Guide to Crontab Scheduling
Scripting03 MIN

Hello**,** everyone**.** In this article, I will discuss a handy feature of Linux**:** “Crontab**.”** It is used to schedule tasks and is one of the most useful features for server administrators.

Some tasks, like backups and updates, need to be repeated repeatedly. This becomes harder when you have more than one server to manage.

To solve this problem, crontab a time/date/month/hour that you specify.

So, let us learn how to use crontab and schedule your tasks.

Crontab quick reference

Fields are: minute, hour, day of month, month, day of week, then the command. Use absolute paths in scripts, redirect logs, and test commands manually before scheduling.

Opening Crontab

To open crontab, then open your terminal and type

bash
crontab -e

If you are using it for the first time, then it will show you something like this -

You can use any text editor you want. I prefer Vim (the second option), but if you are a newbie to Linux, you can use the Nano editor (the first option).

If your task requires admin privileges to execute the command, you must open crontab using this command

bash
sudo crontab -e

Note — crontab -e and sudo crontab -e both are representing a different file.

Basic Usage

Crontab has a particular syntax to declare any task to be scheduled.

Syntax

bash
* * * * * <command-to-be-executed> <arguments>

So, in the above syntax, every position in which * is present represents something.

Here are the details of what each position means in the above syntax.

In the syntax, some symbols are also used. These are:

  1. * - Represents Every
  2. , -Value List Separator
  3. - - Range Of Values
  4. /- Step Values

Examples

So let us have some examples -

Command to run a script every minute

bash
* * * * * db_backup.sh

The above command will run the backup script every minute.

Command to run a script at 00:00 every day

bash
0 0 * * * db_backup.sh

Command to run a script at 5 am every day

bash
0 5* * * db_backup.sh

Command to run the script every Sunday at 5 am

bash
0 5 * * SUN db_backup.sh

The above command can also be written as

bash
0 5 * * 0 db_backup.sh

Command to run a script on weekdays

bash
* * * * 1-5 db_backup.sh

The above command can also be written as

bash
* * * * MON-FRI db_backup.sh

Command to run the script on starting of every month

bash
0 0 1 * * db_backup.sh

Command to run the script at the beginning of the last month of every year

bash
0 0 1 12 * db_backup.sh

Command to run the script at 10:50 am every day

bash
50 10 * * * db_backup.sh

Command to run the script every 30 minutes

bash
*/30 * * * * db_backup.sh

So these are some examples I have taken to help you understand how to schedule a task using crontab.

Shortcuts

So there above we have learned to write a task in crontab.

  1. @reboot — Run once at startup
  2. @yearly — Run once a year
  3. @annually — Run once a year ( Same as @yearly )
  4. @monthly — Run once a month
  5. @weekly — Run once a week
  6. @daily — Run once a day
  7. @midnight — Run once a day at midnight ( Same as @daily )
  8. @hourly — Run once an hour

Example

bash
@reboot db_backup.sh

Some other commands

  1. To list all cron jobs
bash
crontab -l
  1. To remove all cron jobs
bash
crontab -r
  1. To enter crontab for a particular user
bash
crontab -u <username> -e

This is all about crontab. I hope you have found this article useful. If you have any problems, ask me in the command section.

Written by Shashikant Dwivedi

Engineer, occasional writer, full-time noticer. Based in Prayagraj, India. New essays land roughly twice a month.

Keep reading

Adjacent essays.

All writing →

The newsletter

New articles in your inbox.

Occasional articles on engineering, tooling, and software development practices. No marketing, no fluff — just the article, when it's ready.

Unsubscribe with one click. Your email never leaves the list.