This article is about the Cron Job and how to schedule task on Ubuntu machine using Cron jobs. The cron is a time-based job scheduler in Unix-like computer operating systems.

The word "Crontab" is derived from chronos (Time in Greeks) and tab stands for table. Crontab is useful for scheduling the task automatically run at regular interval as the background process. Crontabs are useful for taking backups, synchronizing files, etc.

Before doing all other stuffs, first we will change the default editor to nano editor by typing the following command in terminal.
export EDITOR="nano"
To schedule new /edit crontab entries for specific time
crontab -e
This will open the nano editor with the default commented scheduling with five stars to allow our task scheduling. we can schedule our tasks here as follows
* * * * * /home/harishshan/example.sh
This will execute the example.sh for every minute. The following is an easy way to remember the crontab format
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
If you leave star(asterisk), it means every.If you leave ?, it means any.

Examples:

Schedule Crontab Syntax
Daily 10AM 0 10 * * *
October 12th 12.20 PM 20 12 12 10 *
5 After 4 Every Sunday 5 4 * * 0
Every 15 minutes */15 * * * *
Every Friday 1 AM 0 1 * *5
  1. To list crontab entries
  2. crontab -l
    
  3. To list scheduled cron jobs of a particular user
  4. crontab -u root -l
    
  5. To remove all crontab entry
  6. crontab -r
    
  7. To prompt before deleting crontab
  8. crontab -i -r
    
Note: By default crontab saves the output in user's mailbox which we configured as MAILTO in the crontab
MAILTO="yourname@yourdomain.com"
Instead you can configure to output to separate logfile like as follows
* * * * * /home/harishshan/example.sh >> /var/log/example_log.log 2>&1
Where
  1. /var/log/example_log.log represents log file
  2. 1 represents STDOUT
  3. 2 represents STDERR