Linux Cron and crontab

Source: Internet
Author: User

Cron

Crond is located in/etc/rc. d/init. d/crond or/etc/init. D or/etc/rc. d/rc5.d/s90crond, always reference/var/lock/subsys/crond.

 

Cron is a scheduled execution tool in Linux (equivalent to scheduled tasks in Windows). It can regularly run task tasks without manual intervention. Since cron is a Linux Service (deamon), you can start or disable this service in the following ways:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

You can also enable the service automatically when the system starts:
Add:
/Sbin/service crond start

Now the cron Service is in the process and we can use it.

 

Binary crontab

Crontab is located in/usr/bin/crontab.

 

The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // delete a user's cron Service
Crontab-E // edit a user's cron Service

For example, to view your cron settings as root: crontab-u root-l
For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R
When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e to enter the VI editing mode. The edited content must comply with the following formats:

*/1 ***** ls>/tmp/ls.txt
The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:

Minutes (0-59)
Hour (0-23)
Date (1-31)
Month (1-12)
Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

Every morning
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

Every two hours
0 */2 *** echo "have a break now.">/tmp/test.txt

Every two hours from PM to am, am
0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt

Am from Monday 4 to Wednesday every week
0 11 4*1-3 command line

Am, January 1, January 1
0 4 1 1 * command line

After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The Cron information of this user is recorded in this file, this file cannot be edited directly. You can use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

 

3. Edit the/etc/crontab configuration file
The cron system level configuration file is located in/etc/crontab.

The cron service reads not only all files in/var/spool/cron every minute, but also the/etc/crontab configuration file once, therefore, we can use the cron service to configure this file. The configuration with crontab-E is for a user, while editing/etc/crontab is for system tasks. The file format of this file is:

Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.
Home = // path of the user running. The root directory is used here.

# Run-Parts

01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. Weekly // execute the script in/etc/cron. Weekly every week
42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

 

Four instances

--------------------------------------

Basic Format: [parameters must be separated by spaces]
* *** Command
Hour, day, month, and week commands

The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

Some examples of crontab files:

30 21 ***/usr/local/etc/rc. d/Lighttpd restart
The above example indicates restarting Lighttpd at every night.

45 4, 10, 22 **/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that the Lighttpd is restarted at on the 1st, 10th, and 22th of each month.

10 1 ** 6, 0/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd at every Saturday and Sunday.

0, 30 18-23 ***/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd every 30 minutes between and every day.

0 23 ** 6/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd at every Saturday.

**/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd every hour

* 23-7/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd every hour between PM and PM.

0 11 4 * Mon-wed/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd from every Monday to Wednesday on the 4th of each month.

0 4 1 Jan */usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd at on January 1, January 1.

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.