Understanding Crontab

TDhendup
0

 

What is crontab and cron?

Source:ahmadawais.com

The crontab is used to automate all types of tasks on Linux Systems. In the following section, I will be explaining the syntax of the crontab along with other information, so that you better understand to become a system administrator.

The crontab is a list of commands that you wish to run on a regular schedule, and stands for ‘cron table’, as it uses the job scheduler ‘cron’ to execute tasks. ‘cron’ was named after ‘chronos’, the Greek word for time. ‘cron’ is a system process that automates the tasks, according to the schedule-the crontab.  

The format of Linux Crontab Format is

MIN HOUR DOM MON DOW CMD

Where,

·      MIN is a Minute field with the value range of 0-59

·      HOUR is an Hour field with the value range of 0-23

·      DOM as Day of Month with the value 1-31

·      MON as the Month field with the value 1-12

·      DOW as Day of Week with the value 0-6

·      CMD as the Command to be executed

Example

30 08 10 06 * /home/Tshering/back-up

Explanation

The back-up command will be executed on 10th of June (every day of the week i.e., ‘*’), at 8:30 am. Note-The time uses 24 hours format, i.e., 8 for 8 AM and 20 for 8 PM.

Notes:

To view the crontab entries for the currently logged in user, enter the following command

#crontab -l

To view the crontab entries for the particular user, enter the following command

#crontab -u tshering -l

To edit the crontab for the currently logged in user, enter the following command

#crontab -e

 

Examples

* * * * * CMD

This crontab means the CMD will run every minute of every hour throughout the year i.e., CMD will run during all possible unit.

When specifying */5, it means every 5 minutes.

0-10/2 means every 2 minutes in the first 10 minute.

 

00 11, 16 * * * /home/tshering/incremental-backup

This example executes the command ‘incremental-backup’ at 11:00 AM and 16:00 on every day. i.e., 00-0th minute 11, 16-11 AM and 4 PM * (every day) * (every month) * (every day of the week).

 

00 09-18 * * * /home/tshering/bin/systemctl mysqld.service status

00-0th minute 09-18 means 9am, 10 am, 11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm, * every day, * every month, * every day of the week; and the command checks the status of the database-mysql.

 

00 09-18 * * 1-5 /home/tshering/bin/systemctl mysqld.service status

Same like above, but checks the mysql status on all days (except Saturday and Sunday).

 

*/10 * * * * /home/tshering/bin/df -uh

The command checks for disk usage every 10 minutes throughout the year.

 

Apart from the above examples, there are special cases, whereby you can use @followed by a keyword such as reboot, midnight, yearly, hourly.

@yearly 0 0 1 1 *

@daily 0 0 * * *

@hourly 0 * * * *

@reboot (meaning run at the startup)

@yearly /home/tshering/bin/’annual-maintenance’

 

This command will execute the ‘annual-maintenance’ command at 00:00 on 1st January, for every year.

@reboot CMD

This command will execute the specified command once after the machine boots.

 

Post a Comment

0Comments

Will review your comment and get back!

Post a Comment (0)