CentOS6.5 configure cron tasks

Source: Internet
Author: User
Cron is a daemon that can be used to schedule the execution of repeated tasks based on the combination of time, date, month, and week. Cron assumes that the system continues to run. If the system does not run when a task is scheduled, the task will not be executed. To use the cron service, you must install the vixie-cronRPM package and run the crond service. To determine whether the software package is installed, use rpm

Cron is a daemon that can be used to schedule the execution of repeated tasks based on the combination of time, date, month, and week.

Cron assumes that the system continues to run. If the system does not run when a task is scheduled, the task will not be executed.

To use the cron service, you must installVixie-cronRPM software package, and must be runningCrondService. To determine whether the software package is installed, useRpm-q vixie-cronCommand. To determine whether the service is running, use/Sbin/service crond statusCommand.

37.1.1. configure a cron task

The main configuration file of cron is/Etc/crontab, Which includes the following lines:

SHELL=/bin/bash                  PATH=/sbin:/bin:/usr/sbin:/usr/bin                  MAILTO=root                  HOME=/                  # run-parts                  01 * * * * root run-parts /etc/cron.hourly                  02 4 * * * root run-parts /etc/cron.daily                  22 4 * * 0 root run-parts /etc/cron.weekly                  42 4 1 * * root run-parts /etc/cron.monthly

The first four rows are variables used to configure the running environment of the cron task.SHELLThe value of the variable tells the system which shell environment to use (bash shell in this example );PATHVariable defines the path used to execute commands. Cron task output is mailedMAILTOThe username defined by the variable. IfMAILTOThe variable is defined as a blank string (MAILTO = ""), The email will not be sent.HOMEVariables can be used to set the main directory used for executing commands or scripts.

/Etc/crontabEach row in the file represents a task in the format:

minute         hour         day         month         dayofweek         command

 

  • Minute-Minute, any integer between 0 and 59

  • Hour-Hour, any integer between 0 and 23

  • Day-Date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)

  • Month-The month, any integer from 1 to 12 (or the month can be abbreviated as jan or feb)

  • Dayofweek-Any integer between 0 and 7 in a week. here, 0 or 7 represents Sunday (or the abbreviation of a week, such as sun or mon)

  • Command-Command to be executed (the command can beLs/proc>/tmp/procSuch commands can also be the command to execute your own script .)

In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.

The short line (-) between Integers specifies an integer range. For example,1-4It indicates integers 1, 2, 3, and 4.

Specifies a list of values separated by commas. For example,3, 4, 6, 8Indicate the four specified integers.

A forward slash (/) can be used to specify the interval. Add/<Integer>This means that integer can be skipped within the range. For example,0-59/2It can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example,*/3The value can be used in the month field to indicate that a task is run every three months.

The line starting with # is a comment and will not be processed.

If you are/Etc/crontabAs seen in the file, it usesRun-partsScript execution/Etc/cron. hourly,/Etc/cron. daily,/Etc/cron. weeklyAnd/Etc/cron. monthlyScripts in the directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.

A cron task can be added/Etc/cron. dDirectory. Use and/Etc/crontab.

# record the memory usage of the system every monday                  # at 3:30AM in the file /tmp/meminfo                  30 3 * * mon cat /proc/meminfo >> /tmp/meminfo                  # run custom script the first day of every month at 4:10AM                  10 4 1 * * /root/scripts/backup.sh

Example 37-1. crontab

Users other than root users can useCrontabTool to configure cron tasks. All user-defined crontabs are saved in/Var/spool/cronDirectory, and use the user identity to create them for execution. Create a crontab project as a user, log on as the user, and then typeCrontab-eCommandVISUALOrEDITORThe editor specified by the environment variable to edit the crontab of the user. The format and/Etc/crontabSame. After the changes made to crontab are saved, the crontab file is saved according to the user name and written to the file./Var/spool/cron/Username.

Cron Daemon checks every minute/Etc/crontabFile,Etc/cron. d/Directory, and/Var/spool/cronDirectory changes. If changes are found, they are loaded into the memory. In this way, you do not have to restart the Daemon after a crontab file is changed.

37.1.2. control the use of cron

/Etc/cron. allowAnd/Etc/cron. denyFiles are used to restrict the use of cron. The two control files are in the format of one user per line. Spaces are not allowed for both files. If the control file is modified, the cron daemon (Crond) Does not need to be restarted. The control file is read every time you add or delete a cron task.

Regardless of the rules in the control file, the root user can always use cron.

IfCron. allowThe file exists. only the users listed in the file are allowed to use cron, andCron. denyFiles are ignored.

IfCron. allowThe file does not exist.Cron. denyAll users listed in are forbidden to use cron.

37.1.3. start and stop services

To start the cron service, use/Sbin/service crond startCommand. To stop the service, use/Sbin/service crond stopCommand. We recommend that you start the service during boot.

 

Since Cron is a built-in service in Linux, 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

.....

 


Crontab contains cron. d, cron. daily, cron. weekly, cron. monthly, cron. hourly and crontab and cron. deny files under the/etc directory.

Cron. daily is the job executed once a day, and cron. weekly is the job executed once a week.
Cron. monthly is the job executed every month, and cron. hourly is the job executed every hour.

Cron. d is a task automatically and periodically required by the system, but it is not executed by hour, by day, by week, by month, so it is placed under this directory.
If it is executed by hour, by day, by week, or by month, you can put it under the corresponding directory.


Some people may ask, when is the previous job executed by day, by week, and by month automatically?
Check the/etc/crontab file to find out the details:
[Admin @ C2C _std etc] $ cat crontab
SHELL =/bin/bash
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root
HOME =/

# Run-parts
01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly

The/etc/cron. deny file is used to control which users are not allowed to use Crontab.


In addition, there are two directories named cron and anacron under/var/spool.
The cron directory contains the crontab content added by each user through crontab-e.

Under the anacron directory, the last execution time of cron. daily, cron. monthly, cron. weekly is recorded.


The linux cron service reads all the content under/var/spool/cron,/etc/crontab,/etc/cron. d every minute.


In linux, the following files are stored in the/etc/cron. daily directory by default:
-Rwxr-xr-x 1 root 286 Aug 14 2004 tmpwatch
-Rwxr-xr-x 1 root 276 Sep 29 2004 0 anacron
-Rwxr-xr-x 1 root 2133 Dec 1 2004 prelink
-Rwxr-xr-x 1 root 180 Jul 13 2005 logrotate
-Rwxr-xr-x 1 root 121 Aug 9 2005 slocate. cron
-Rwxr-xr-x 1 root 48apr 10 2006 mcelog. cron
-Rwxr-xr-x 1 root 418 Apr 14 2006 00-makewhatis.cron
-Rwxr-xr-x 1 root 104 May 31 2006 rpm
Lrwxrwxrwx 1 root 28 Apr 7 08:25 00-logwatch-> ../log. d/scripts/logwatch. pl

Some of the items here are resource-consuming and can be removed.
Tmpwatch is used to clear temporary files that have not been accessed for a long time. Keep this.
0anacron is used to update the execution time of crontab job, which must be kept. It is 0 before the name to ensure that the program will
Run other programs. And this is automatically called by run-parts.

Prelink is used to pre-link some dynamic linked libraries and executable files to accelerate the startup time and reduce the memory allocation of run-time, which is similar to oracle.
Dbms_shared_pool.keep has the same effect. This is a double-edged sword.

The function of logrotate is used cyclically to compress some system logs.

Slocate. cron is used to update the slocate database. it is used for the locate service and consumes resources and can be stopped.

Mcelog. cron is used to generate the/var/log/mcelog file.

00-makewhatis.cron is used to create whatis database and serve whatis. Resource consumption, which can be removed.
This also exists in cron. weekly, which can be removed.

Rmp stores the rpm package information installed on the system in/var/log/rpmpkgs every day. This is useless and can be removed.

00-logwatch is used to monitor linux log files. Yes.


Related Article

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.