SIMPLE TASK AUTOMATION

 

Crontab

Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis. User can use this to schedule activities, either as one- time events or as recurring tasks.

Scheduling of Tasks (For Ubuntu)

 

Step 1 : Open terminal and type the command crontab –e

Step 2 : Choose the editor. Better to select nano editor Step 3 : Edit the file based on the syntax given above Step 4 : Save and Exit the file

Step 5 : Start cron daemon using the following command

systemctl start cron

 

Linux Crontab Format

 

MIN HOUR DOM MON DOW CMD

 

Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

 

Field

Description

Allowed Value

MIN

Minute field

0 to 59

HOUR

Hour field

0 to 23

DOM

Day of Month

1-31

MON

Month field

1-12

DOW

Day Of Week

0-6

CMD

Command

Any command to be executed

 

 


 

Create a new crontab file, or edit an existing file

# crontab -e [username]

where username specifies the name of the user's account for which you want to create or edit a crontab file.

 

Verify your crontab file changes

# crontab -l [username]

Install crontab

crontab -a filename

Edit the crontab

# crontab -e

Display crontab

crontab -l

Display the last edit the crontab file

crontab -v

Remove crontab

crontab -r

Following are the syntax for cron

minute(s) hour(s) day(s) month(s) weekday(s) command(s) "Argument1" "Argument2" 1 * 3 4 5 /path/to/command arg1 arg2

 

If you don't have parameter put star(*) Commands:

1)   -l  - List or manage the task with crontab command

2)   -e - edit crontab entry.

3)   -u - To list scheduled jobs of a particular user called tecmint using.

4)     -r - parameter will remove complete scheduled jobs without confirmation from crontab.

5)  -i - prompt you confirmation from user before deleting user’s crontab.

 

Allowed special character (*, -, /, ?, #)

 

1.     Asterik(*)        – Match all values in the field or any possible value.

2.     Hyphen(-)        – To define range.

3.     Slash (/)            – 1st field /10 meaning every ten minute or increment of range.

4.     Comma (,)        – To separate items.

                                                          


 

      System Wide Cron Schedule
 

System administrator can use predefine cron directory as shown below.

1.     /etc/cron.d

2.     /etc/cron.daily

3.     /etc/cron.hourly

4.     /etc/cron.monthly

5.     /etc/cron.weekly

6.      

To Schedule a Job for Specific Time

The below jobs delete empty files and directory from /tmp at 12:30 am daily. User need to mention user name to perform crontab command.

In below example root user is performing cron job.

# crontab –e

30 0 * * * root find /tmp -type f -empty –delete

 

Special Strings for Common Schedule

 

Strings

 

Meanings

@reboot

Command will run when the system reboot.

@daily

Once per day or may use @midnight.

@weekly

Once per week.

@yearly

Once per year.user can use @annually keyword also.

 

Multiple Commands with Double ampersand (&&)

To run the command1 and command2 daily

# crontab -e

@daily <command1> && <command2>

 

Scheduling a Job for a Specific Time

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the full backup shell script (full-backup) on 10th June 08:30 AM.

The below time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20. 30 08 10 06 * /home/username/full-backup

The below time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20. 30 08 10 06 * /home/username/full-backup

           


 

                  30 – 30th Minute

                  08 – 08 AM

                  10 – 10th Day

                  06 – 6th Month (June)

                  * – Every day of the week

Schedule a Job for More Than One Instance (e.g. Twice a Day)

The following script takes a incremental backup twice a day every day. This example executes the specified incremental backup shell script (incremental-backup) at 11:00 and 16:00 on every day. The comma separated value in a field specifies that the command needs to be executed in all the mentioned time.

00 11,16 * * * /home/username/bin/incremental-backup

                  00 – 0th Minute (Top of the hour)

                  11,16 – 11 AM and 4 PM

                  * – Every day

                  * – Every month

                  * – Every day of the week

 

Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)

        To schedule the job for every hour with in a specific range of time then use the following.

Cron Job everyday during working hours

This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/username/bin/check-db-status

        00 – 0th Minute (Top of the hour)

        09-18 – 9 am, 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

Schedule a Job for Every Minute Using Cron.

Ideally user may not have a requirement to schedule a job every minute. But understanding this example will help user understand the other examples mentioned below in this article.

 

* * * * * CMD

 

The * means all the possible unit — i.e every minute of every hour throughout the year. More than using this * directly, user will find it very useful in the following cases.

 

                  When user specify */5 in minute field means every 5 minutes.

                  When user specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute.

                  Thus the above convention can be used for all the other 4 fields


Table: Cron special keywords and its meaning

 

Keyword

Equivalent

@yearly

0 0 1 1 *

@daily

0 0 * * *

@hourly

0 * * * *

@reboot

Run at startup.

 

Schedule a Job for First Minute of Every Year using @yearly

User can specify a job to be executed on the first minute of every year, then user can use the @yearly cron keyword as shown below.

This will execute the system annual maintenance using annual-maintenance shell script at 00:00 on Jan 1st for every year.

@yearly /home/username/red-hat/bin/annual-maintenance

Schedule a Cron Job Beginning of Every Month using @monthly

Executes the command monthly once using @monthly cron keyword.

This will execute the shell script tape-backup at 00:00 on 1st of every month. @monthly /home/username/suse/bin/tape-backup

Schedule a Background Job Every Day using @daily

Using the @daily cron keyword, this will do a daily log file cleanup using cleanup-logs shell scriptat 00:00 on every day.

@daily /home/username/arch-linux/bin/cleanup-logs "day started"

To Execute a Linux Command After Every Reboot using @reboot

Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

 

@reboot CMD

To Disable/Redirect the Crontab Mail Output using MAIL keyword

By default crontab sends the job output to the user who scheduled the job. To redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below.

username@dev-db$ crontab -l MAIL="username"

@yearly /home/username/annual-maintenance

*/10 * * * * /home/username/check-disk-space

[Note: Crontab of the current logged in user with MAIL variable]

 

To stop the crontab output to be emailed, add or update the MAIL variable in the crontab as shown below.

MAIL=""                                                                                                                                                                       

 

 

 

 


 

To View Crontab Entries:

View Current Logged-In User’s Crontab entries To view crontab entries type

crontab -l

 

Username@dev-db$ crontab -l

@yearly /home/username/annual-maintenance

 

*/10 * * * * /home/username/check-disk-space

 

[Note: This displays crontab of the current logged in user]

 

To View Root Crontab entries

 

Login as root user (su – root) and do crontab -l as shown below. root@dev-db# crontab -l

no crontab for root

 

To View Other Linux User’s Crontabs entries:

To view crontab entries of other Linux users, login to root and use -u {username} -l

root@dev-db# crontab -u username -l @monthly /home/username/monthly-backup

00 09-18 * * * /home/username/check-db-status

 

To Edit Crontab Entries:

Edit Current Logged-In User’s Crontab entries To edit a crontab entries,

use crontab –e

By default this will edit the current logged-in users crontab. username@dev-db$ crontab -e

@yearly /home/username/centos/bin/annual-maintenance

*/10 * * * * /home/username/debian/bin/check-disk-space "/tmp/crontab.XXXXyjWkHw" 2L, 83C

[Note: This will open the crontab file in Vim editor for editing.


 

   QUESTIONS FOR PRACTICE:

 

Q1. Schedule a task to display the following message on the monitor for every 2 minutes.

 

 

 

 

 

 

 

 

 

 

Q2. Schedule a task to take backup of your important file (say file f1) for every 30 minutes

 

 

 

 

 

 

Q3. Schedule a task to take backup of login information everyday 9:30am

 

 

Post a Comment

Previous Post Next Post