crontab command for the Linux Task Scheduler

Source: Internet
Author: User
Tags date1

The crontab command is commonly used in UNIX and Linux operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.

In Linux, Linux task scheduling is mainly divided into the following two categories:
1, the system performs the work: the system periodically to perform the work, such as the backup system data, cleans up the cache
2, Personal work: a user to do regular work, such as every 10 minutes to check the mail server for new letters, the work can be set by each user

I./etc/crontab,/etc/cron.deny,/etc/cron.allow file Introduction

The task of the system scheduling is usually stored in the/etc/crontab file, which contains some system running scheduler, through the command we can look inside the content:

/etc/ crontab shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# for details see Mans 4 crontabs# Example of Job definition:#.----------------minute (0-59) # |  -------------Hour (0-23) # |  |  . ----------Day of Month (1-31) # |  |  |  . -------Month (1-12) OR jan,feb,mar,apr ... # | | |  |  ----Day of Week (0-6) (sunday=0 or 7) or sun,mon,tue,wed,thu,fri,sat# | | | | |  # * * * * * *  user-name command to be executed

These tasks will be automatically dispatched after the system is run. /etc/cron.deny and/etc/cron.allow files are also stored in the/etc directory.

/etc/cron.deny represents a user who cannot use the crontab command

/etc/cron.allow represents a user who can use crontab.

If two files exist at the same time, then/etc/cron.allow is preferred.

If none of the two files exist, then only the root user can schedule the job.

Ii.. crontab command syntax and the meanings of each parameter

The syntax for the crontab command is:

Note: Crontab is used to allow a user to execute a program at a fixed or fixed interval, in other words, a user-like time table. -u user refers to the setting of the time table for the specified user, which presupposes that you must have permission (for example, root) to specify another's time schedule. If you do not use the-u user, it means setting your own schedule.

Description of each parameter:

-E [UserName]: Perform a text editor to set the time table, the default text editor is vi-r [UserName]: Delete the current schedule table-l [UserName]: List the current schedule table-V [UserName]: List The status of the user cron job

Iii. format of the crontab command

If we want to create a task scheduler of our own, we can use the CRONTAB-E command, such as:

[Email protected] ~]# CRONTAB-E

This will go to the VI editing interface, to write the task we want to dispatch, the format of the Crontab Dispatch command is as follows:

* * * * * command path//First five fields can take an integer value, specify when to start working, the Sixth field is a string, that is, the command field, which includes the command executed by the Crontab Dispatch. Each field is segmented with spaces and tabs.

Rules for dispatching commands:

Field name Description Range of values
Minutes Hours of the first minute execution 0-59
Hours The number of hours of the day is executed 0-23
Date Day of the Month execution 1-31
Calendar The first month of the year is executed 1-12
Week Day of the Week execution 0-6
Command name Commands and parameters to execute

Some common special symbols in the crontab command:

Symbol Description
* Indicate any moment
, Represents a split
Represents a segment, as in the second paragraph: 1-5, which means 1 to 5 points
/n Indicates that each n unit executes once, as in the second paragraph, */1, which indicates that the command is executed every 1 hours. Can also be written as 1-23/1.

Some examples of CRONTAB commands are given below:

XX 8,12,16 * * */data/app/scripts/monitor/df.sh30 2 * * */DATA/APP/SCRIPTS/HOTBACKUP/HOT_DATABASE_BACKUP.SH10 8,12,16 * * */DATA/APP/SCRIPTS/MONITOR/CHECK_IND_UNUSABLE.SH10 8,12,16 * * */DATA/APP/SCRIPTS/MONITOR/CHECK_MAXFILESIZE.SH10 8,12,16 * * * * * * * * * * * * * * * */data/app/scripts/monitor/check_objectsize.sh  21:43 Execution 15 05 * * * Daily 05:15 Execution 0 17 * * * Daily 17:00 Execution 0 17 * * 1 per Monday 17:00 Execution 0,10 17 * * 0,2,3 every Sunday, Tuesday, Wednesday 17:00 and 17:10 execute 0-10 17 1 * * Each month 1st from 17:00 to 7:10 every 1 minutes to perform 0 0 1,15 * 1 each month 1st and 15th and day of the 0:00 execution 42 4 1 * * Every month 1st 4:42 minutes Execution 0 21 * * 1-6 weeks to Saturday 21:00 Execute 0,10,20,30,40,50 * * * * 10 minutes Execute */10 * * * * * every 10 minutes * 1 * * * * from 1:0 to 1:59 every 1 minutes 0 1 * * * 1:00 Execution 0 */1 * * * 0 minutes per 1 hours Line 0 * * * * 0 minutes every 1 hours Execution 2 8-20/3 * * 8:02,11:02,14:02,17:02,20:02 Execution 30 5 1,15 * * 1st and 15th 5:30 implementation

Iv. creating our own crontab command

① instance One: If I want to write the system's time to the Date1.txt file in the/home directory every minute, enter the following command

[Email protected] ~]# CRONTAB-E

Enter the crontab command in the crontab command Edit interface: * * * * * Date >>/home/date1.txt
(The first five * * * * * * indicates that the time is every minute the Date command is the current system time, the >> command means that the result is added to the file after the > command means that the result overwrites the file)
If crontab:installing new crontab appears after saving, the Dispatch command has been successfully

We can go to the/home directory to see if the text file already exists, and view the information inside:
[[email protected] home]# cat date1.txt Sat Apr  6 16:15:09 CST 2013Sat Apr  6 16:16:02 CST 2013Sat Apr  6 16:17: 2013 CST

At this point we find that the crontab command we have just written has run normally, and this command will continue as long as there is no terminating command or system shutdown.

② Example Two: If we need to complete two commands simultaneously, that is, every minute the system time is written to the Date1.txt file in the/home directory, and every other minute the Date1.txt file is copied to/directory to generate a date2.txt file. What do we usually do then? There are two ways of doing this:

A) The simplest and most straightforward method ( not recommended ): Enter the CRONTAB-E command directly and append a command to the following, such as:

[[email protected] ~]# crontab-e* * * * * Date >>/home/date1.txt* * * * * cp/home/date1.txt/date2.txt

Finally we exit to save, at this time we can find/directory already has date2.txt this file, open can see its content and date1.txt like:

[[email protected]/]# cat Date2.txtsat Apr  6 16:15:09 CST 2013Sat Apr  6 16:16:02 CST 2013Sat Apr  6 16:17:01 C  ST 2013Sat Apr  6 16:18:01 CST 2013Sat Apr  6 16:19:01 CST 2013Sat Apr  6 16:20:01 CST 2013Sat Apr  6 16:21:01 CST 2013Sat Apr  6 16:22:01 CST 2013Sat Apr  6 16:23:02 CST 2013

Although this method is relatively simple, it is not recommended because it is not convenient to maintain if there are many commands and if I need different users to execute different commands, here is another method.

b) First write an executable sh file, and then write the task we want to perform in the Sh file, and finally execute our SH file via Crontab ( recommended )

First we write a task.sh file in the/directory, the contents of which are the two commands we have just executed:

[Email protected]/]# VI task.sh

task.sh inside the command
Date >>/home/date1.txt
Cp/home/date1.txt/date3.txt

At this point our task.sh is not an executable file, we can see it through the ls-l command

-rw-r--r--.   1 root root    54 April   6 16:27 task.sh//tash.sh is not enforceable, we want to modify its permissions

So we need to modify the permissions of task.sh through the chmod command:

[Email protected]/]# chmod 744 task.sh

At this point, you can see that task.sh is already an executable file:

-rwxr--r--.   1 root root    54 April   6 16:27 task.sh

Then we enter the CRONTAB-E command, delete the previous two commands, or comment out the previous # number:

#* * * * * Date >>/home/date1.txt#* * * * * cp/home/date1.txt/date2.txt* * * * */task.sh

At this point we found that the root directory already exists in the Date3.txt file, open can be found inside the content is:

[[email protected]/]# cat Date3.txtsat Apr  6 16:15:09 CST 2013Sat Apr  6 16:16:02 CST 2013Sat Apr  6 16:17:01 C  ST 2013Sat Apr  6 16:18:01 CST 2013Sat Apr  6 16:19:01 CST 2013Sat Apr  6 16:20:01 CST 2013Sat Apr  6 16:21:01 CST 2013Sat Apr  6 16:22:01 CST 2013Sat Apr  6 16:23:02 CST 2013Sat Apr  6 16:24:01 CST 2013Sat Apr  6 16:25  : 2013Sat Apr  6 16:26:01 CST 2013Sat Apr  6 16:27:01 CST 2013Sat Apr  6 16:28:01 CST 2013Sat Apr  6 16:29:01 CST 2013Sat Apr  6 16:31:02 CST 2013Sat Apr  6 16:32:01 CST 2013

V. Some other commands of crontab

If we need to list what commands we have created for a task schedule, you can use the Crontab-l command to view

[[email protected]/]# crontab-l#* * * * * Date >>/home/date1.txt#* * * * * cp/home/date1.txt/date2.txt* * * * * /task.sh

If we need to terminate the task schedule we just created, use the Crontab-r command to

[Email protected]/]# Crontab-r

At this point, we can see that the mission in Crontab is gone.

Vi. Startup of Cron Services

We can pass Chkconfig--list | grep cron command to see the startup of the Cron service:

[Email protected] home]# Chkconfig--list | grep croncrond              0: Off    1: Off    2: Enable 3: Enable 4: Enable 5:    enable    6: Off

We can see that the system boot level if the 1-4,cron service will start automatically, we can set the startup entry for the service by the following command:

/sbin/service crond start/sbin/service crond stop/sbin/service crond restart/sbin/service crond reload above 1-4 lines are start, stop, Restart the service and reload the configuration. To set cron to start automatically when booting, add/sbin/service crond start in the/etc/rc.d/rc.local script.

Ext.: http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/06/3002602.html

crontab command for the Linux Task Scheduler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.