Crontab details --

Source: Internet
Author: User
Tags syslog rsyslog

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:
[[Email protected] _ 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.

 

 

In ubuntu, store the default cron logs in/var/log/cron.

1. Modify the rsyslog file:/etc/rsyslog. d/50-default.conf

Delete # Before # cron. * In the rsyslog file;

2. Restart the rsyslog service: Service rsyslog restart

3. Restart the cron service: Service cron restart

4. view the log file: tail-F/var/log/cron. Log

 

In ubuntu, cron logs exist:

By default the cron log in Ubuntu is located at/var/log/syslog. Use the below command to check the cron entries in this file.

Grep cron/var/log/syslog

Run grep cron/var/log/syslog

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.