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 |
- To list crontab entries
- To list scheduled cron jobs of a particular user
- To remove all crontab entry
- To prompt before deleting crontab
crontab -l
crontab -u root -l
crontab -r
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>&1Where
- /var/log/example_log.log represents log file
- 1 represents STDOUT
- 2 represents STDERR
Comments
Post a Comment