Scheduling Task In Linux Using Crontab

Shashikant Dwivedi
SKDBLOG
Published in
3 min readJun 2, 2020

--

Hello everyone In this article I am going to discuss a very useful feature of Linux — “Crontab”. It is used to schedule tasks. It is one of the most useful features for server administrators.

Some tasks are to be done again and again, like backups and updates. It becomes harder when you have more than one server to manage.

To solve this problem crontab comes into action. It will help you to run tasks in a time/date/month/hour that you specify.

So let us begin learning how you can use crontab and schedule your tasks.

Opening Crontab

To open crontab open your terminal and type

crontab -e

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

Choose any text editor that you want. I prefer vim (2nd option), but if you are newbie Linux uses nano editor (1st option).

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

sudo crontab -e

Notecrontab -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

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

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

Here is the detail 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

* * * * * db_backup.sh

The above command will run the backup script every minute.

Command to run a script at 00:00 every day

0 0 * * * db_backup.sh

Command to run a script at 5 am every day

0 5* * * db_backup.sh

Command to run the script every Sunday at 5 am

0 5 * * SUN db_backup.sh

The above command can also be written as

0 5 * * 0 db_backup.sh

Command to run a script in weekdays

* * * * 1-5 db_backup.sh

The above command can also be written as

* * * * MON-FRI db_backup.sh

Command to run the script on starting of every month

0 0 1 * * db_backup.sh

Command to run the script on starting of last month of every year

0 0 1 12 * db_backup.sh

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

50 10 * * * db_backup.sh

Command to run the script after every 30 minutes

*/30 * * * * db_backup.sh

So these are some examples that I have taken you to get an understanding of how to schedule a task using crontab.

Shortcuts

So there above we have learned to write a task in crontab. There are some shortcuts that you can also use.

  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 in midnight ( Same as @daily )
  8. @hourly — Run once an hour

Example:

@reboot db_backup.sh

Some other commands

To list all the crontab jobs

crontab -l

To remove all crontab jobs

crontab -r

To enter crontab for a particular user

crontab -u <username> -e

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

--

--

Shashikant Dwivedi
SKDBLOG

I am full time developer, working for DeviceDoctor.IN. I write articles on topics that I learn daily by doing.