Linux crontab Command to Create and Manage Cron Jobs

Filed Under: UNIX/Linux

Linux crontab command is used to schedule cron jobs. A cron job is a handy automation tool that enables Linux users to execute automated tasks at specific periods or intervals.

This is a friendly way of scheduling tasks which would otherwise be tedious if managed manually. Cron jobs are usually used by system administrators to automate tasks such as server backups, restarts, sending of notifications, and clean-ups to mention just but a few.

A cron job runs silently in the background, checking the /etc/crontab file and /var/spool/cron and /etc/cron.*/ directories. In this article, you will learn how to create and manage cron jobs in your Linux system.

Linux Cron Job Structure

The anatomy of a cron job is as shown below.

* * * * * COMMAND arg1 arg2

OR

* * * * * /path/to/script arg1 arg2

From the left,


The 1st entry represents Minutes [0 - 59]
The 2nd entry represents Hours   [0 - 24]
The 3rd entry represents Days    [0 - 31]
The 4th entry represents Months  [0 - 12]
The 5th entry represents Days of the week [0 - 7]

Linux crontab Example

Now that you have the skeleton for a crontab, writing one is quite easy provided you know the command or the path to your script.

In this example, we are going to create a crontab for rebooting a Linux system.

vim reboot.sh

Copy and paste the content below into the bash file.


#!/bin/bash

0 3 * * * /sbin/reboot

Going by the crontab syntax as earlier indicated, the cron job will run daily at 3:00 am and reboot the system.

Save and quit the text editor.

Next, assign the execute permissions as shown.

# chmod +x reboot.sh

To verify file permissions, run:

# ls -l reboot.sh

To initialize the crontab run

# crontab reboot.sh

If you want to view contents of a crontab of a user that is currently logged in user run the command.

crontab -l

Check Running Crontabs

With the crontab up and running, the crontab will be executed at the stipulated time, i.e 3:00 am local time.

To remove all cron jobs in the system, use the -r option.

crontab -r

Additional examples of running cron jobs at certain time intervals

  1. To run a cron job every minute, run
    * * * * * COMMAND

    If the current time is 6:00 am, the cron job will run at 6:01 am, 6:02 and so on.

  2. To run a cron job after every nth minute, for example after every 10 minutes, run
    */10 * * * * COMMAND
  3. To run a cron job every nth minute of every hour, for example after every 30 minutes of every hour, execute
    30 * * * * COMMAND
  4. To run a cron job every hour, i.e every 0 minutes, run
    0 * * * * COMMAND
  5. To run a cron job after every n hours, for instance, after every 6 hours, run
    0 */6 * * * COMMAND
  6. To run a cron job at a specific day, say Friday, execute
    0 0 * * FRI COMMAND
  7. This cron job will be executed every Friday at exactly 00:00 midnight.

  8. If you wish to run a cron job from Monday to Fridayrun
    0 0 * * 1-5 COMMAND

    This executes the command at 00:00 hours every day from Monday to Friday.

  9. To execute a cron job everyday, run
    0 0 * * * COMMAND
  10. To execute a cron job every first day of every month
    0 0 1 * * COMMAND
  11. To execute a cron job every first day of the month at a specific time, say 20:30 hours, run
    30 20 1 * * COMMAND
  12. To run a job every quarter of the year, i.e. the 1st day after every 3 months, run
    0 0 1 */3 * COMMAND
  13. To run a cron job every year, run
    0 0 1 1 * COMMAND

Scheduling cron jobs using Crontab Guru

Crontab syntaxes can be quite confusing and difficult to master. But don’t worry. Crontab Guru is a very friendly online tool that helps you seamlessly define your time intervals without much hassle. It’s a web interface that has crontab’s time intervals well labeled and all that is required is for you to type in the figures into the text field as the tool interprets the crontab. In the example below, the tool generated the crontab for automating a cron job every 6:00 am from Monday to Saturday.

Crontab Guru

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages