Linux System Planning Task two: Cron task scheduling

Source: Internet
Author: User
Tags name database

Connect to one of the Linux system scheduled tasks: at command


System in the daily operation, often need to make some cycle of backup or inspection work. This regular cycle of work tasks, in the Linux system is implemented with Crond system services. Crond is a daemon that is used to periodically perform certain tasks or wait for certain events under Linux, which is open by default on the CentOS system.

[Email protected] ~]# chkconfig--list crondcrond 0:off1:off2:on3:on4:on5:on6:off


The task scheduling under Linux is divided into two categories, namely system-level task scheduling and user-level task height. The following are described separately.


1. System-Level task scheduling


The tasks that the Linux system performs periodically, such as automatic online update (on-line update), automatic updatedb update file name database, etc. This work schedule can be done by editing the global profile/etc/crontab (this is a plain text file, not to be confused with the/usr/bin/crontab executable file). The contents of the file are as follows:

[[email protected] ~]# vim /etc/crontabshell=/bin/ bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# for details see man 4  CRONTABS# EXAMPLE OF JOB DEFINITION:#&NBSP---------------- minute  (0 -  59) # |  .------------- hour  (0 - 23) # |  |   .---------- day of month  (1 - 31) # |  |  |  . ------- month  (1 - 12)  OR jan,feb,mar,apr ...# |  |   |  | &NBSP---- day of week  (0 - 6)   (sunday=0 or  7)  or sun,mon,tue,wed,thu,fri,sat# |  |  |  |  |#  *  *  *  *  * user-name command to be  Executed 

The first four lines of the file are environment variable information that configures the cron task to run. As the following table:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/0B/wKioL1dpV3aB3QELAAAcNEPbC14837.png "title=" 1.png " alt= "Wkiol1dpv3ab3qelaaacnepbc14837.png"/>


The content that begins with # in a file is a specific description of the configuration format of the task.


The configuration of the task is as follows: The user executes the instruction content in a total of 7 subsections.

The meaning of the parameter is shown in the following table:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/0C/wKiom1dpWKTwNMYOAAAXHBgsD2k354.png "title=" 2.png " alt= "Wkiom1dpwktwnmyoaaaxhbgsd2k354.png"/>

Data 0 and 7 of the week of the 5th column represent Sunday.

The values of the first five columns can also contain some auxiliary characters, such as the following table:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/83/0B/wKioL1dpWLygwnY9AAAocTgeIZY879.png "title=" 3.png " alt= "Wkiol1dpwlygwny9aaaoctgeizy879.png"/>

The 6th column is the execution user. If this column is empty, it will cause the configured routine to not run.

The 7th column is the directive to be executed. There are two ways to do this, one for direct instruction, and one that is commonly used in Linux systems to specify a directory in which certain types of script files are stored.

A Instruction Form

If the system records a memory allowance every 5 minutes and writes it in the file.

*/5 * * * * root grep "^memfree"/proc/meminfo >>/tmp/memory.log


B Specify directory mode

Write the script you want to execute and save it to a specific directory. As below, edit the file

[Email protected] ~]# vim/tmp/cron/backupetc.sh #!/bin/bash tar-jcf/backup/etc_ ' date ' +%f_%t '. tar.xz/etc/*

#保存后修改文件模式. Note that script files in the directory must have specific executable permissions. Otherwise, it cannot be run.

[Email protected] ~]# chmod a+x/tmp/cron/backupetc

Edit the/etc/crontab file and add the following:

0 4 * * * Root Run-par ts/tmp/cron/

The line content setting system automatically executes all the script files under directory/tmp/cron every 4 o'clock, including the script for files in the auto-backup/etc directory as defined above.

In/etc directory, the system has set the cron.daily, Cron.hourly, cron.monthly, cron.weekly directory information. As a management specification, you can also place scripts that perform tasks in the first 4 corresponding directories.


2. User-level task scheduling


Users who have periodic work to do, such as to the user data backup or maintenance personnel regularly check the status of the server and email reminders. Users can use the crontab command to customize their task scheduling.

If the user Zeng log in, edit the task content using the CRONTAB–E directive.

[Email protected] ~]$ crontab–e

User task scheduling is configured in the same format as the system-level Task Scheduler global file/etc/crontab, except that the 6th column execution user is not specified, as described in the previous section. Enter the following and save the exit.

#该例子只用开测试

*/5 * * * * echo "now is: ' Date '" >>/tmp/usercron.log

Each task row, you can enter multiple tasks. The content entered in this command is automatically saved to the/var/spool/cron/zeng file.

[Email protected] ~]# Cat/var/spool/cron/zeng

*/5 * * * * echo "now is: ' Date '" >>/tmp/usercron.log

The content of user-level task scheduling is compared with the system-level scheduling content, but the execution user is less written and the other is the same.


Additional parameter descriptions for crontab

-U: Only the parameters that the root can release can specify the user who created, queried, or deleted crontab information

-L: View user crontab content, default is the currently logged in user

-E: Edit the contents of the crontab file

-r: Delete the contents of the crontab file

such as querying the user's task scheduling job

[[Email protected] ~] #crontab-u zeng-l# Display Content */2 * * * * echo "now is: ' Date ' +\%f \%t '" >>/tmp/usercron.log


Linux System Planning Task two: Cron task scheduling

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.